0
如何在使用Ajax單擊「Submit Post」按鈕時編寫用於添加帖子的javascript?此外,javascript文件將被稱爲index.js.haml或create.js .haml? 以下是相關文件:使用AJAX更新軌道視圖中的帖子
index.html.haml:
.post#new_post
- @posts.each do |post|
= post.body
= render partial: "form"
_form.html.haml:
= bootstrap_form_for @post, remote: true do |f|
= f.text_area :body, label: "", required: true
= f.submit "Submit Post", class: "button"
帖子控制器(索引和創建方法):
def index
load_posts # a method I use for loading posts
end
def create
if @post.save
respond_to do |format|
format.js
format.html do
flash[:success] = "Saved."
end
end
else
load_posts
flash.now[:error] = "Sth went wrong. "
render :index
end
end
JS爲ajaxifying:
$('#new_post').append('#{escape_javascript(render(@post))}');
非常感謝!
太感謝你了..它的作品! :) – user3477051 2014-08-29 19:00:40