Ruby on rails Rails有“许多:通过在父级上创建关联和关联模型”;“显示”;看法
我的rails 5应用程序中有三个模型。。。我想做的是能够在此人的“显示”视图中创建一个便笺,然后便笺应该自动链接到此人以及我选择附加便笺的任何其他人 这就是我要去的地方 注Ruby on rails Rails有“许多:通过在父级上创建关联和关联模型”;“显示”;看法,ruby-on-rails,has-many-through,Ruby On Rails,Has Many Through,我的rails 5应用程序中有三个模型。。。我想做的是能够在此人的“显示”视图中创建一个便笺,然后便笺应该自动链接到此人以及我选择附加便笺的任何其他人 这就是我要去的地方 注 课堂笔记
课堂笔记
人员注释(联接表)
class PersonNote
人
class Person < ApplicationRecord
has_many :person_notes
has_many :notes, through: :person_notes
accepts_nested_attributes_for :person_notes
accepts_nested_attributes_for :notes
end
class-Person
在我的“人员”控制器的“显示”部分,我有以下内容:
def show
@notep = Note.new()
@person.notes << @notep
end
def显示
@notep=Note.new()
@person.notes如果我理解正确,您将需要一个单独的notes控制器和create操作来处理note创建。然后在“人员显示”视图中,您可以添加提交给NotesController#create的表单和输入字段。如果我理解正确,您将需要一个单独的Notes控制器和create操作来处理备注创建。然后在People show视图中,可以添加一个表单和输入字段,提交给NotesController#create。确保正确获取@person变量的person记录。你能告诉我你面临的具体错误吗?谢谢你的帮助丹尼尔,我没有收到错误。。。问题是1。我不知道该怎么做。2.每当我打开person记录时,都会自动创建一个note记录(以及与person的关联)……请确保您正确地获取了@person变量的person记录。你能告诉我你面临的具体错误吗?谢谢你的帮助丹尼尔,我没有收到错误。。。问题是1。我不知道该怎么做。2.每当我打开个人记录时,都会自动创建一个笔记记录(以及与个人的关联)……我有一个笔记控制器,我可以从人物模型的“显示”页面创建笔记而不会出现问题。但是,我无法让它自动将此人链接到便笺@person.notes我有一个便笺控制器,我可以从人物模型的“显示”页面创建便笺而不会出现问题。但是,我无法将此人自动链接到便笺@person.notes
class Person < ApplicationRecord
has_many :person_notes
has_many :notes, through: :person_notes
accepts_nested_attributes_for :person_notes
accepts_nested_attributes_for :notes
end
def show
@notep = Note.new()
@person.notes << @notep
end
<%= simple_form_for(@notep, remote: true) do |f| %>
<%= f.error_notification %>
<%= f.input :subject %>
<%= f.input :note %>
<%= f.input :date, html5: true %>
<div class="input-field">
<%= f.check_box :isalert, :id => "alert" %>
<%= f.label :isalert, :for => "alert" %>
</div>
<div class="input-field">
<%= f.check_box :archived, :id => "archived" %>
<%= f.label :archived, :for => "archived" %>
</div>
<div class="input-field">
<%= f.check_box :deleted, :id => "deleted" %>
<%= f.label :deleted, :for => "deleted" %>
</div>
<%= f.input :datecreated, html5: true %>
<%= f.input :user_id %>
<%= f.simple_fields_for :person_notes do |builder| %>
<%= builder.association :person, label_method: :lstfstfullname %>
<%= builder.input :deleted %>
<%= builder.input :datecreated, html5: true %>
<%= builder.input :user_id %>
<% end %>