How to transfer data partially?

K guys, so I created this up / down vote script (basically, like here in stackoverflow), and I'm trying to add Ajax to it so that the page does not reload every time you vote,

I have two controllers, one of which is called grinders, and one is called voting. (Grinding machines are mostly messages)

So, here is the index of all the crushers (looks like this) Screenshot

and here is the code for this 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 every time I try to vote for him, I get the following error:

You have zero if you did not expect this! Perhaps you were expecting an array instance. Error evaluating nil.each Screenshot of error. I tried a lot of things and I just can't get it to work! (Enlarged screenshot - http://grab.by/7bgb )

Any help? If you need more information, just ask!

+4
source share
4 answers

To transfer data in partial, use the locals parameter.

 <%= render(:partial => "grinders/grinders", :locals => {:grinders => @grinders})%> 

And then in partial, label it as grinders , not @grinders .

However, since you are presenting the collection here, this is an idiomatic way to render the collection:

 <%= render :collection => @grinders %> 

This will look for the partial name "grinder.erb", and the local variable to be passed in to partial will be grinder (a unique name for the collection). With this, you can get rid of the partial loop operator. You can also use your own name, for example my_grinder , and a local variable with the same name will be passed in partial.

To learn more, follow these steps: http://guides.rubyonrails.org/layouts_and_rendering.html

+16
source

In Rails 3, it looks like

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, individual instances of the part have access to a member of the collection, which is rendered through a variable named after the partial. In this case, _product is partial, and inside the _product part you can reference the product to get an instance that is displayed

+2
source

Try <%= render(:partial => "grinders/grinders", :object => @grinders) %>

0
source

It looks like your Votes # create action is trying to handle the partial processing of "grinders / _grinders.html.erb", which is strange because AJAX calls will most likely output JS or JSON.

Can you show us the contents of the files:

  • application / views / votes / create.rjs.erb
  • app / controllerlers / vote_controller.rb (especially the creation method)
0
source

All Articles