2

I was going through a video and where he was rendering something like below example:

<div>
 <%= render posts %> # no quotes to posts
</div>

Though he has even created a partial with _posts.html.erb, he is calling with quotes to posts.

Though he has mentioned something about it like it calls, active record model, then class and then something...i could not understand it properly. Can anyone explain clearly this with simple example.

Rahul Dess
  • 2,397
  • 2
  • 21
  • 42

2 Answers2

4

Render with quotes

<%=render 'post'%>

Rails is going to look in the current folder for a partial file which starts with _

Render without quotes

Is still going to use the same partial, but post in this case is a variable. I think this is translating to this:

<%= render partial: "post", locals: {any_string: your_variable(in this case is post)} %>

Again I haven't checked that.

The _post.html.erb is the partial, which can look like this:

      <b><%=any_string%></b>

If your_variable which was assigned to any_string will contain the string 'My name is'

Your partial will print 'My name is' in bold.

Anyway partial are more complex, and they are used for DRY-ing (Don't repeat yourself) the code.

You can see more examples here.

alexsmn
  • 1,736
  • 11
  • 18
  • im sorry, i dint understand much, can you tell me in easy terms – Rahul Dess Jul 01 '14 at 15:45
  • there is a lot of documentation about partials, the best way for you to learn is by trying it yourself. Anyway which bit you don't understand? – alexsmn Jul 01 '14 at 15:55
1

With quotes then you are explicitly rendering a partial of that name. Without quotes something quite interesting is happening. posts (without quotes) is a variable that will be an activemodel list of records.

Now what the call to render does is it will look at the type of each of the models and then find the correct partial for the model (which will be the name of the model camel_cased) and render each one in turn.

EDIT:

If you have a model called Post and you assign some of those records to a variable (he uses posts I assume but I'll use foo to disambiguate) like so:

foo = Post.all

then by calling render foo the render function will see that you have an activerecord collection of records, it will then check the model associated with these records (Post in our example) and will loop through all of them rendering them to a partial called _post.html.erb with a local variable for each record assigning the record to post.

<%= render foo %>

is equivalent to:

<% foo.each do |my_post| %>
  <%= render partial: "post", locals: {post: my_post} %>
<% end %>
Mike H-R
  • 7,726
  • 5
  • 43
  • 65
  • if i mention render posts, it will search for model called posts ? – Rahul Dess Jul 01 '14 at 16:19
  • i almost got it, just want to make sure that my understanding is clear, `render post` will look for variable inside Post model and then executes the query associated with that variable.. and then that results are rendered through our posts partial ? – Rahul Dess Jul 01 '14 at 16:34
  • 1
    No, `render post` doesn't execute any query. that happens earlier when you assign something to the `posts` variable. The thing that you assign will be the result of a query (such as `Post.all` as in my example). Then calling render on this variable (which is posts in your example, foo in mine to stop you getting confused) the render function checks the model associated with these records (`Post` as we got it from `Post.all` and then looks for a partial called `_post.html.erb` and passes the record into a local variable in that partial and renders it. – Mike H-R Jul 01 '14 at 16:48