Alexandre
Alexandre

Reputation: 13308

Rails and ajax - passing values to partial view

Rails 3.2.3

I need to insert many rows for a table by clicking a link using AJAX. It's working perfect but there is an issue. When I pass the value @paged_photo to partial it's null there. However it's definitely not null, it contains a data.

#home/index.html.erb

<table   id="photos_table">
  <%= render :partial => 'test_partial'  %>
</table>
  <%= link_to "More photos",  {:controller => "home", :action => "test_method" }, :remote => true %>

#home/_test_partial.html.erb; #there are 3 td for each row #@paged_photos is null here for some reason

<tr>
<td>
   <%= link_to :controller => 'photos', :action => 'show', :id=>@paged_photos[0].id do %>
       <%=image_tag(@paged_photos[0].source)%>
   <%end%>
</td>
<td>
   <%= link_to :controller => 'photos', :action => 'show', :id=>@paged_photos[1].source do %>
       <%=image_tag(@paged_photos[1].source)%>
   <%end%>
</td>
<td>
   <%= link_to :controller => 'photos', :action => 'show', :id=>@paged_photos[2].id do %>
       <%=image_tag(@paged_photos[2].source)%>
   <%end%>
</td>
</tr>

#home/test_method.js.erb

$('#photos_table').append('# <%=j render :partial => 'test_partial'%>');

controller

   def test_method
        @paged_photos = get_photos        
        respond_to() do |format|
          format.js
        end
     end

How do I pass the value to partial to insert a row for a table?

Upvotes: 0

Views: 1118

Answers (1)

Kulbir Saini
Kulbir Saini

Reputation: 3915

Can you try to use the following in your home/test_method.js.erb?

$('#photos_table').append('<%=j render :partial => 'test_partial', :paged_photos => @paged_photos %>');

After this, access paged_photos in home/_test_partial.html.erb.

If that works, you'll have to modify your home/index.html.erb like this

<%= render :partial => 'test_partial', :paged_photos => @paged_photos %>

PS : I didn't understand the use of # before <%=j in home/test_method.js.erb. Was it just a typo or it's intentional?

Upvotes: 1

Related Questions