简体   繁体   English

在Rails中关联模型?

[英]Associated models in Rails?

In my rails application I have two models called Kases and Notes. 在我的rails应用程序中,我有两个名为Kases和Notes的模型。 They work in the same way comments do with blog posts, Ie each Kase entry can have multiple notes attached to it. 它们以与博客帖子评论相同的方式工作,即每个Kase条目都可以附加多个注释。

I have got everything working, but for some reason I cannot get the destroy link to work for the Notes. 我已经完成了所有工作,但是由于某种原因,我无法获得销毁链接来为Notes工作。 I think I am overlooking something that is different with associated models to standard models. 我认为我忽略了关联模型与标准模型不同的东西。

Notes Controller 笔记控制器

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

Kase Model 加濑模型

class Kase < ActiveRecord::Base
  validates_presence_of :jobno
  has_many :notes

Note Model 音符模型

class Note < ActiveRecord::Base
  belongs_to :kase
end

In the Kase show view I call a partial within /notes called _notes.html.erb: 在Kase show视图中,我在/ notes内称一个名为_notes.html.erb的partial:

Kase Show View 加濑秀场

<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 /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 %>

As you can see, I have a Remove Note destroy link, but that destroys the entire Kase the note is associated with. 如您所见,我有一个“删除注释”销毁链接,但是销毁了与该注释关联的整个Kase。 How do I make the destroy link remove only the note? 如何使销毁链接仅删除注释?

<%= link_to "Remove Note", kase_path(@kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

Any help would, as always, be greatly appreciated! 一如既往,任何帮助将不胜感激!

Thanks, 谢谢,

Danny 丹尼

<%= link_to "Remove Note", note_path(note), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

you also will need the following entry in config/routes.rb (check if it already exists) 您还需要在config / routes.rb中输入以下内容(检查是否已存在)

map.resources :notes

and check for following method in your NotesController 并在NotesController中检查以下方法

def destroy
  @note = Note.find(params[:id])
  @note.destroy
  .... # some other code here
end 

there's also another way of doing that if you don't have a NotesController and don't want to have it 如果您没有NotesController也不想拥有它,还有另一种方法

You're calling the delete method on a kase -t hat's why it's deleting a kase. 您在kase上调用delete方法-这就是为什么要删除kase的原因。 There's nothing in this link 此链接中没有任何内容

<%= link_to "Remove Note", kase_path(@kase), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

apart from the text that even mentions a note - so why would it delete a note? 除了甚至提到笔记的文字之外,为什么还要删除笔记呢? Try 尝试

<%= link_to "Remove Note", note_path(note), :confirm => 'Are you sure?', :method => :delete, :class => 'important' %>

This assumes you have the standard restful routes and actions set up. 假设您已经设置了标准的静态路由和操作。

As an additional point, you should never use link_to for non-get actions, because 另外,永远不要对非获取操作使用link_to,因为

  1. google spiders and the like will click on them. 谷歌蜘蛛之类的将点击它们。 You might say 'they can't because you need to be logged in' and that's true but it's still not a good idea. 您可能会说“他们不能,因为您需要登录”,这是事实,但这仍然不是一个好主意。
  2. if someone tries to open the link in a new tab/window it will break your site, or go to the wrong page, since it will try to open that url but with a get instead of a delete. 如果有人尝试在新标签页/窗口中打开链接,它将破坏您的网站,或进入错误的页面,因为它将尝试打开该URL,但使用get而不是delete。
  3. generally, in web design, links should take you somewhere and buttons should 'do stuff', ie make changes. 通常,在网页设计中,链接应将您带到某个地方,而按钮应“做些事”,即进行更改。 A destructive action like this therefore belongs on a button not a link. 因此,像这样的破坏性行为属于按钮而不是链接。

Use button_to instead, which constructs a form to do that same thing. 请改用button_to,这将构造一个表单来执行相同的操作。
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002420&name=button_to http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002420&name=button_to

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM