Julia’s Blog

puts “my thoughts here”

Rendering a Notice in Rails

Last week we had our science fair at the Flatiron School. It was nerve wracking, but an awesome and fun experience. The week prior was grueling. Whilst spending hours with my team building our app, I noticed something interesting about notices and errors.

The first place I rendered a notice was in the Sessions Controller. It was plain and simple, no problem.

1
2
3
4
def destroy
  session[:user_id] = nil
  redirect_to root_path, :notice => "You've been signed out!"
end

Great! Onto the next message, an error in my Landings Controller:

1
2
3
4
5
6
7
8
9
def create
  @user = User.find(params[:user_id])
  @landing = Landing.new(landing_params)
  if @landing.save
    redirect_to user_landing_path(@user, @landing)
  else
    render "new", :error => "Please fill in all fields!"
  end
end

But wait, this notice did not render. The render method doesn’t take an alert/notice/error hash entry like the redirect_to method does. So, how do we make a flash appear on “render”?

1
2
3
4
5
6
7
8
9
10
def create
  @user = User.find(params[:user_id])
  @landing = Landing.new(landing_params)
  if @landing.save
    redirect_to user_landing_path(@user, @landing)
  else
    flash.now[:error] = "Please fill in all fields!"
    render "new"
  end
end

Not too much hassel. We had to add one extra line before the render - flash.now. This makes the notice render on the current page.

Total DOMination

Last week at the Flatiron school, we took the dive into Javascript and jQuery to create more inactive and dynamic webpages. Javascript and jQuery interact with something called the DOM - the document object model. The DOM is a representation of HTML in a hierarchal tree-like structure, or an API for HTML. It allows javascript and jQuery to quickly find HTML elements. Every browser is able to generate a DOM, but how? Glad you asked…

The first thing we will need to construct the DOM is some HTML.

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
<body>

<h1>Hello world</h1>

<p>Blah blah blah.</p>

</body>
</html>

Upon receiving the HTML, the browser will tokenize, or convert strings into tokens. Adhering to the W3C HTML5 Standard, it will take strings inside brackets and create tokens, for example the <html> will be converted into an html token. Tokens look like: start tag: html,start tag: body, start tag: h1,end tag: h1, start tag: p, end tag: p,end tag: body, end tag: html. These tokens will be then converted into nodes by means of a lexer. A lexer is a type of parser. And from these nodes, a DOM tree will be constructed. The browser will be able to determine parents and children from the start and end tags. For example since the start tag: body is situated between the start tag: html and end tag: html, the browser will know that the body node is a child of the html node.

DOM

APIs

What is an API??

Lately I’ve been hearing a lot about APIs - so I decided to look into what they really are and how they work. An API, short for Application Program Interface, is basically a set of instructions for building software application. The way I see it, APIs make life easier for programmers who make life easier for users of software applications.

Although APIs help enormously with improving the user experience on many websites, they are completely invisible to the user. The main job of an API is to allow software to interact with other software, they silently work their magic in the background while users only see a pretty user interface.

Gimme Some Syntactic Sugar

As a beginner programmer, learning a new language can be scary and daunting. So, I was delighted to learn about “syntactic sugar”.

The term “syntactic sugar” sounds sweet and inviting, and it is! It is syntax that does not follow the regular conventions and it makes for easier writing and reading of code. Ruby was designed with it’s users in mind so its syntax is rather natural and not much different from the way we actually speak at times. Syntactic sugar makes Ruby (and other languages) more user (and beginner) friendly!

Life with no sugar…

1
2
3
4
2.+(5)
x=x+1
"hello world".send(:capitalize)
a||a=b

A spoon full of sugar helps the code look much prettier…

1
2
3
4
2+5
x+=1
"hello world".capitalize
a||=b

Wow, look how much clearer and “more human” the code looks with a touch of sugar!

Unfortunately, not everyone is a fan of syntactic sugar. There are critics that see these exceptions as rather unimportant.

“Syntactic sugar causes cancer of the semicolon.” - Alan Perlis