0

ページを更新せずに実行時に新しい投稿を追加して、Rails で Ajax を実装しています。問題は、このスクリプトで割り当てられた特定のビューをレンダリングしたいことですcreate.js.erb

 $('<%=render(:file => "posts/post.html.erb)%>').insertBefore($('.posts').children('div.post-box').first());

しかし、うまくいきません。このスクリプトの何が問題なのか、特定のビューから jquery で html コードを選択する方法を誰か教えてもらえますか?

そしてこれがcreateアクションです

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
4

1 に答える 1

0

必要なのはescape_javascript、このようにメソッドを追加することだけです

$('<%= escape_javascript(render(:file => "posts/post.html.erb"))%>').insertBefore($('.posts').children('div.post-box').first());
于 2012-07-04T02:41:37.917 に答える