1

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.

1 Answer 1

4

I got something similar to work like this:

format.html { render "posts/_posts", :locals => { :posts => @posts } }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.