K guys, so I created this up/down vote script (basically like the one here on stackoverflow), and I'm trying to add some Ajax to it, so that the page doesn't reload everytime you vote.
I have two controllers, one called grinders, and one called votes. (Grinders are basically posts)
So here's the index of all the grinders (Looks like this )
and here's the code to that page.
</head>
<body>
<h1>Listing grinders</h1>
<%= render(:partial => "grinders/grinders")%>
<br />
<%= link_to 'New grinder', new_grinder_path %>
</body>
</html>
and this is what I have in the views/grinders/_grinders.erb
<% @grinders.each do |grinder| %>
<div id="main">
<div style="float:left; height:80px; width:50px">
<div class='up'>
<% form_for(@vote, :remote => true) do |u|%>
<%= u.hidden_field :grinder_id, :value => grinder.id %>
<%= u.hidden_field :choice, :value => "up" %>
<%= image_submit_tag("http://i13.photobucket.com/albums/a287/Rickmasta185/arrow-small-green-up.png", :class => 'create') %>
<% end %>
</div>
<center><%= grinder.votes_sum %></center>
<div class='down'>
<% form_for(@vote, :remote => true) do |d|%>
<%= d.hidden_field :grinder_id, :value => grinder.id %>
<%= d.hidden_field :choice, :value => "down" %>
<%= image_submit_tag("http://i13.photobucket.com/albums/a287/Rickmasta185/arrow-small-red-down.png", :class => 'create') %>
<% end %>
</div>
</div>
<div class='box' >"<strong>It grinds our gears </strong><%=h grinder.grinder %>"</div>
</div>
</div>
<% end %>
But everytime I try to vote for one, I get the following error:
You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each I've tried soo many things, and I just cannot get it to work! (Larger screenshot - http://grab.by/7bgb)
Any help? If you need anymore information, just ask!
For passing data to a partial, use the locals
option.
<%= render(:partial => "grinders/grinders", :locals => {:grinders => @grinders})%>
And then in your partial, refer it as grinders
and not @grinders
.
However, since you are rendering a collection here, this is the idiomatic way of rendering a collection:
<%= render :collection => @grinders %>
This will look for a partial named 'grinder.erb' and the local variable that will be passed to the partial, would be grinder
(Singular name for the collection). With this, you can actually get rid of the loop statement in your partial. You can also use a custom name, say my_grinder
, and a local variable with the same name will be passed to the partial.
To know more, go through this: http://guides.rubyonrails.org/layouts_and_rendering.html
In Rails 3 it is like this
index.html.erb
<h1>Products</h1>
<%= render :partial => 'product', :collection => @products %>
_product.html.erb
<p>Product Name: <%= product.name %></p>
When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is _product, and within the _product partial, you can refer to product to get the instance that is being rendered
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With