0

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.

1
  • here is a good book to get you started. Give it a read and if in doubt then ask questions: railstutorial.org. Also see: inf.ufpr.br/calj08/… Commented Oct 24, 2014 at 8:43

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

1
  • 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.

3 Comments

Before anything, thank you. "...then you have to tell your controller that's the response has to be in json format..." ---> render json: @book, then?
I sense some follow up questions coming up :)
render is another part of this story. In short, render method tells your controller action, what data should be returned as a request response to the client. render json: @book renders serialized book as JSON object. See also guides.rubyonrails.org/layouts_and_rendering.html#using-render .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.