Reputation: 15094
I am building a small twitter-like feed. A user has an input, and you have a feed of inputs.
The feed is effectively a list of posts. Each post's view is defined by a _post.html.haml
partial view.
I have an AJAX post method that creates a new post in the backend. The problem is that you have to refresh the page to actually view the post appear on the feed.
I would like to append the html to the feed column. However, I create a post on the feed column using a partial view that takes in a local variable of the post details.
Partial views are on the server side. How can I still use the same partial view to obtain the html code for the new post I have created so that I can append it to feed column without having to refresh the page.
Thanks!
Upvotes: 1
Views: 3477
Reputation: 10137
Post code snippets so it can easy to answer. Anyway you can do like this. When you are creating a new post using get ajax request it will go into your controller action. In controller action you can do:
def get_new_post #This is the controller action called by your ajax
post = Post.create!(ajax_params)
render :partial => "post", :locals => {:post => post}
end
In view you append the new post to post list as below:
$.get("ajax_request", function(data, status){
if(status=="success"){ //if your ajax is success
$("#post-list").append(data) //Assuming post-list is the div id of list of posts
}
});
Upvotes: 1
Reputation: 2743
Try render(:update) in the action you post to:
render(:update) do |page|
page.replace_html 'divID', :partial => 'your-partial'
end
Upvotes: 1