new to rails and really new to jquery. I've set up some AJAX page rendering by adding respond_to :html, :js to my controller and respond_to @Posts in posts#index method. It render's my views/posts/_posts.html.erb partial using AJAX, however, I needed the html response to direct to the same place so I tried adding a respond_to do |format| block.
posts#index
@posts = ...
respond_to do |format|
format.html { render 'posts/_posts' }
format.js { respond_with @posts }
This works and sends my my :html response to the correct view, but I have a problem with variables. My posts#index uses an @posts variable, but my views/posts/index.js.erb files does this:
$('#stream').html('<%= escape_javascript(render "posts", :posts => @posts) %>');
So my views/posts/_posts.html.erb looks for posts instead of @posts
<% posts.each do |post| %>
<%= render 'shared/feed_item', :feed_item => post %>
<% end %>
I tried sending the local variable from the controller like below but this didn't work:
posts#index
@posts = ...
respond_to do |format|
format.html { render 'posts/_posts', :posts => @posts }
format.js { respond_with @posts }
Any ideas how to send get this working? Thanks a lot.