【发布时间】:2017-12-20 03:32:13
【问题描述】:
我有一个coaching_notes 索引的一部分,其中包含一个用于创建笔记的表格。我想创建一个指导说明并在不刷新页面的情况下进行部分更新。我收到一个未知操作错误:找不到 CoachingNotesController 的操作“显示”。如果我添加 show 操作,我会收到缺少模板的错误。当我尝试从控制器中删除 format.html 部分时,我也遇到了未知格式错误。
我在 Rails 文档之后对我的代码进行了建模,并且还查看了一堆类似的情况,但无法让它工作。
非常感谢任何帮助!!!
/views/coaching_notes/_index.html.erb
<div id="coaching_notes">
<% @coaching_notes.each do |note| %>
<li> <%= note.content %> </li>
<% end %>
</div>
<br>
<% @coaching_note = CoachingNote.new(user_id: current_user.id, meeting_id: session[:current_meeting_id]) %>
<%= form_for(@coaching_note, remote: true) do |f| %>
<%= f.hidden_field :user_id %>
<%= f.hidden_field :meeting_id %>
<%= f.hidden_field :archive %>
<%= f.text_field :content %>
<%= f.submit %>
<% end %>
/views/coaching_notes/_coaching_note.html.erb
<li><%= note.content %></li>
/views/coaching_notes/create.js.erb
$("<%= escape_javascript(render coaching_note) %>").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
2017 年 7 月 15 日更新 - 我根据@Cira 的回答修改了几个文件:
/views/coaching_notes/_index.html.erb
<div id="coaching_notes">
<% @coaching_notes.each do |note| %>
<li> <%= note.content %> </li>
<%= link_to "delete", note, :remote => true, :method => :delete, :data => {:confirm => "Are you sure?"} %>
<% end %>
</div>
<br>
<% @coaching_note = CoachingNote.new(user_id: current_user.id, meeting_id: session[:current_meeting_id]) %>
<%= form_for(@coaching_note, remote: true) do |f| %>
<%= f.hidden_field :user_id, id: 'user' %>
<%= f.hidden_field :meeting_id, id: 'meet' %>
<%= f.hidden_field :archive, id: 'arch' %>
<%= f.text_field :content, id: 'content' %>
<%= f.submit 'Add note', id: 'submit_note' %>
<% end %>
// newly added
<script>
$('#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 )
}
});
})
</script>
(我不知道的主要部分是带有数据属性的成功函数)
/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 in CoachingNotes#create - undefined method `each' for nil:NilClass' 在coaching_notes/index.html 中突出显示这一行
<% @coaching_notes.each do |note| %>
然后我得到“缺少模板 coaching_notes/index”
我觉得我们越来越近了!任何帮助表示赞赏!
【问题讨论】:
标签: jquery ruby-on-rails ajax ruby-on-rails-4