在我的rails应用程序中,我有两个模型叫做Kases和Notes。他们以与博客文章评论相同的方式工作,即I.e.每个Kase条目可以附加多个注释。Rails中的相关模型?
我已经得到了一切正常,但由于某种原因,我无法获得破坏链接为Notes工作。我认为我忽视了与标准模型相关的模型有所不同。
注意控制器
class NotesController < ApplicationController
# POST /notes
# POST /notes.xml
def create
@kase = Kase.find(params[:kase_id])
@note = @kase.notes.create!(params[:note])
respond_to do |format|
format.html { redirect_to @kase }
format.js
end
end
end
加濑型号
class Kase < ActiveRecord::Base
validates_presence_of :jobno
has_many :notes
注意型号
class Note < ActiveRecord::Base
belongs_to :kase
end
在加濑秀鉴于我打电话/ Notes中的部分称为_notes.html.erb:
加濑显示视图
<div id="notes">
<h2>Notes</h2>
<%= render :partial => @kase.notes %>
<% form_for [@kase, Note.new] do |f| %>
<p>
<h3>Add a new note</h3>
<%= f.text_field :body %><%= f.submit "Add Note" %>
</p>
<% end %>
</div>
/notes/_note.html.erb
<% div_for note do %>
<div id="sub-notes">
<p>
<%= h(note.body) %><br />
<span style="font-size:smaller">Created <%= time_ago_in_words(note.created_at) %> ago on <%= note.created_at %></span>
</p>
<%= link_to "Remove Note", kase_path(@kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>
</div>
<% end %>
正如你可以看到,我有一个删除注释销毁链接,但是破坏了该注释关联的整个Kase。我如何使销毁链接只删除笔记?
<%= link_to "Remove Note", kase_path(@kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>
任何帮助将一如既往,非常感谢!
感谢,
丹尼
太棒了,这是我错过的map.resources。 Woops! 谢谢! – dannymcc 2010-06-07 09:39:25