Bick
Bick

Reputation: 18521

Questions about Rails render call

When seeing in the code the following lines :

<%= render :partial => 'ingredients/form',
           :locals => {:form => recipe_form} %>

I wonder what is actually happening.

  1. I have noticed render is a part of RenderingHelper. How do I know which object I can use while writing? How can I use it without RenderingHelper prefix as in Java (i.e. RenderingHelper.render). Am I inheriting from it in the form view?

  2. Since I know Java and C#, I have searched for ingredients/form and couldn't find it in the code. I am guessing it is part of the convention over configuration rule. What is it? Where is it defined?

  3. Regarding the :locals => {:form => recipe_form} line, is that a parameter sent to render? Is that double assignment? What does the => operator actually do here?

  4. If it is a parameter it was hard to understand from the render signature:

    Returns the result of a render that's dictated by the options hash. The primary options are:

    • :partial - See ActionView::PartialRenderer.
    • :file - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
    • :inline - Renders an inline template similar to how it's done in the controller.
    • :text - Renders the text passed in out.

Upvotes: 0

Views: 162

Answers (2)

Dave Newton
Dave Newton

Reputation: 160181

2. "ingredients/form" isn't code--it's a string. It refers to the _form partial for the ingredient model. It's located in app/views/ingredients/_form.erb.html.

3. :locals => { ... } means the render function is getting a map entry with the symbol :locals as its key. render knows what to do with that map.

It's not "double assignment", it's that the value of the :locals key is also a map. =>, or the "hash rocket", is Ruby 1.8/1.9 syntax for assigning a map value to a map key. In Ruby 1.9 you can use a shortcut, locals: { ... }.

(IMO which is more readable depends on the values--when the values are keys, I think the new syntax is heinous, otherwise I like it.)

4. How is the signature difficult to understand? The argument is a hash of options.

Upvotes: 0

user229044
user229044

Reputation: 239250

In the future, please try to ask one question per post.

  1. RenderingHelper is a module, and its methods are made available via an include somewhere. Read more about how Ruby's modules work.
  2. Partial filenames are always prfixied with an underscore, your partial will be located in app/views/ingredients/_form.html.erb
  3. Both :form and :locals are keys in an options hash being passed to render; The corrpsponding value for :locals is a nested hash, which contains one key, :form whose value is a local variable named recipe_form. This might be more obvious if you explicitly specify some of the optional punctuation:

    <%= render({:partial => 'ingredients/form', :locals => {:form => recipe_form}}) %>
    
  4. Ruby (and Rails specifically) use variable length lists of arguments in the form of key/value hashes. They aren't hard to use or understand at all once you understand the basic syntax

Upvotes: 1

Related Questions