Tito
Tito

Reputation: 298

Ruby on Rails basic tips

Im new at Ruby on Rails language and I really need that someone explain me some 'topics' if possible. I've created an app and I Scaffolded it and it created in the controller lots of code but I have doubts. One of them is:

This app is 'empty' so far. It only has a 'New Book' in the first page.

//books\index.html.erb

||| <%= link_to 'New Book', new_book_path %> |||

new_book_path redirects me to books_controller

def new @book = Book.new

respond_to do |format| //-----> What means this 'format'?  
  format.html # new.html.erb // What really mean two options for 'format'?
  format.json { render json: @book } // What means render json: @book
end

# new.html.erb -> has this code inside

New author

/*<%= render 'form' %>

<%= link_to 'Back', authors_path %>*/

Can someone explain me what's hapenning here?

I know that these are really silly questions but I'm not getting it.

Thanks in advance.

Upvotes: 0

Views: 120

Answers (2)

evilguc
evilguc

Reputation: 930

The best two ways to understand Rails in-depth are reading its code (https://github.com/rails/rails) and reading Documentation (http://api.rubyonrails.org and http://guides.rubyonrails.org). So you'll find enough information to cover this topic here: http://api.rubyonrails.org/classes/ActionController/MimeResponds.html or here: http://guides.rubyonrails.org/action_controller_overview.html.

But if you want short answer... listen to the story :) The entire respond_to do ... end block is responsible for defining rules on how your app should response on different 'formats'. Rails supports a lot of different formats, i.e :html, :json, :xml (you even can define your own formats). Beside mime types it has variants: :desktop, :tablet, :phone. Obviously that with mime types you describe how you want to answer on different types of request and with variants you specify different options for various user agents. :format variable passed into block has type ActionController::MimeResponds::Collector. They didn't call it so for nothing. It collects all different response types you specify inside block and then using headers section from http request picks an appropriate variant from that options. Hope it was useful. But again, better check Documentation.

Rails uses MVC pattern in as its foundation ([http://en.wikipedia.org/wiki/Model–view–controller]). So what we've seen before was a Controller. And you can treat new.html.erb as a View for :new action for that controller. The file itself is an html file flavored with ERB (NOT the same as Epic Rap Battles of History, but [http://en.wikipedia.org/wiki/ERuby]) template engine. ERB is able to inject chunks of ruby code into your pages. <% %> enclosing tag is used just for evaluation and <%= %> for injection of the result of evaluation. So in your case with <%= render 'form' %> you inject result of #render method call into your html and with :link_to helper you create link.

IN CONCLUSION: I recommend you to start with https://www.railstutorial.org. That's an excellent tutorial for starters. You'll find answers for most of your questions and even develop your own little Twitter! (at least 2nd edition is about Twitter).

Upvotes: 2

blelump
blelump

Reputation: 3243

  • respond_to is a Rails controller method (explanation here: http://api.rubyonrails.org/classes/ActionController/MimeResponds.html#method-i-respond_to), which gets a block as argument. In short, block is a part of code ran within method it has been passed to.
  • For a block you declare variable called 'format'. Because this in just variable name so you may declare it i.e. 'f' or whatever you want.
  • Within the block of respond_to method, you may declare how your controller action responds for given MIME type. So, for HTML you may leave it empty, however if you want your controller to respond to JSON (MIME: application/json and you define it in the request header from the client side), then you have to tell your controller that's the response has to be in json format.

Upvotes: 1

Related Questions