我有一个部分的coaching_notes索引和一个用于创建备注的表单。我想创建一个教练笔记,并进行部分更新而不刷新页面。我收到一个未知动作错误:CoachingNotesController无法找到'show'动作。如果我添加显示操作,我会收到缺少的模板错误。当我尝试从控制器中删除format.html部分时,我也遇到了未知格式错误。Ajax和Rails 4:创建实例变量并更新视图而不刷新
我在Rails文档之后模拟了我的代码,并回顾了一堆类似的情况,但无法使其工作。
任何帮助非常感谢!
/views/coaching_notes/_index.html.erb
/views/coaching_notes/_coaching_note.html.erb
/视图/ coaching_notes /创建。 js.erb
$("").appendTo("#coaching_notes");
/controllers/coaching_notes_controller.rb
def create
@coaching_note = CoachingNote.new(coaching_note_params)
respond_to do |format|
if @coaching_note.save
format.html { redirect_to @coaching_note, notice: 'note was successfully created.' }
format.js {}
format.json { render json: @coaching_note, status: :created, location: @coaching_note }
else
format.html { render action: "new" }
format.json { render json: @coaching_note.errors, status: :unprocessable_entity }
end
end
的routes.rb
resources :coaching_notes
UPDATE 17年7月15日 - 我已经根据修改的情侣档@ Cira的回答:
/views/coaching_no TES/_index.html.erb
true, :method => :delete, :data => {:confirm => "Are you sure?"} %>
// newly added
$('#submit_note').on("click", function() {
var u = $('#user').val()
var m = $('#meet').val()
var a = $('#arch').val()
var c = $('#content').val()
$.ajax({
url: "/coaching_notes/create",
type: "POST",
data: {
user_id: u,
meeting_id: m,
archive: a,
content: c
},
dataType: "json",
success: function(info) {
$('#coaching_notes').data("html", info)
}
});
})
(,我不知道的主要部分是成功的功能与数据属性)
/controllers/coaching_notes_controller.rb
def create
@coaching_note = CoachingNote.new(coaching_note_params)
@html = render_to_string :partial => "coaching_notes/index"
respond_to do |format|
if @coaching_note.save
format.html
format.json { render json: {"html" => @html} }
else
format.html { render action: "new" }
format.json { render json: @coaching_note.errors, status: :unprocessable_entity }
end
end
两件事: 1.我的成功功能在ajax中看起来是否正确? 2.我需要将if @coaching_note.save放在控制器中吗?
的错误,现在我得到的是“NoMethodError在CoachingNotes#创建 - 未定义的方法`每次”的零:NilClass'强调此行/ index.html中
然后我得到coaching_notes“失踪模板指导/索引'
我觉得我们正在接近!任何帮助表示赞赏!
2017-07-14
SVC