I’m Implementing Ajax with Rails by adding a new post in runtime without refreshing the page.
The problem is I want to render a specific view by this script which is allocated in create.js.erb
$('<%=render(:file => "posts/post.html.erb)%>').insertBefore($('.posts').children('div.post-box').first());
But it doesn’t work, Can anyone tell me what’s wrong on this script and how to select html code in jquery from a specific view?
and this’s the create action
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to :back, notice: 'Post was successfully created.' }
format.js
end
end
All you need is add
escape_javascriptmethod, just like this