如何使用accepts_nested_attributes_for在Rails3中创建嵌套对象?

发布于 2024-10-24 06:47:01 字数 1215 浏览 1 评论 0原文

我不知道如何设置一个表单来创建新的 Study,同时创建相关的 StudySubjectFacilityuser_idfacility_idstudy_subject_id 必须可用才能创建 Study 对象,如您在数据库中看到的那样关系模型。

数据库模型

这是研究的迁移。其他表不包含外键。

def self.up
 create_table :studies do |t|
  t.references :user
  t.references :facility
  t.references :subject
  t.date "from"
  t.date "till"
  t.timestamps
 end
 add_index :studies, ["user_id", "facility_id", "subject_id"], :unique => true
end

这些模型定义了以下关联。

# user.rb
has_many :studies

# subject.rb
has_many :studies

# facility.rb
has_many :studies

# study
belongs_to :user
belongs_to :subject
belongs_to :facility

问题

1) has_manybelongs_to 定义正确吗?
2) 如何使用 accepts_nested_attributes_for研究一个>?
3) 一项研究只能属于一个用户。我是否需要将 user_id 添加到每个其他对象中来存储关联?

经过两周的广泛学习,我对 Rails 完全陌生。抱歉,可能问了一个愚蠢的问题。

I cannot figure out how I can setup a form that will create a new Study while also creating the related StudySubject and the Facility. The user_id, facility_id and study_subject_id have to be available to create the Study object as you can see in the database relation model.

Database model

Here is the migration for the studies. The other tables do not contain foreign keys.

def self.up
 create_table :studies do |t|
  t.references :user
  t.references :facility
  t.references :subject
  t.date "from"
  t.date "till"
  t.timestamps
 end
 add_index :studies, ["user_id", "facility_id", "subject_id"], :unique => true
end

The models define the following associations.

# user.rb
has_many :studies

# subject.rb
has_many :studies

# facility.rb
has_many :studies

# study
belongs_to :user
belongs_to :subject
belongs_to :facility

Questions

1) Are the has_many and belongs_to definitions correct?
2) How can I create a study using accepts_nested_attributes_for?
3) A study should only belong to one user. Do I need to add the user_id into every other object to store the association?

I am totally new to Rails since 2 weeks of extensive learning. Sorry for a stupid question maybe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-10-31 06:47:01

是的。有用。一位好朋友提供了帮助。这就是我们设置的。
请注意,我同时将 StudySubject 重命名为 Subject

模型 study.rb code>

belongs_to :student, :class_name => "User", :foreign_key => "user_id"  
belongs_to :subject  
belongs_to :university, :class_name => "Facility", :foreign_key => "facility_id"  

accepts_nested_attributes_for :subject, :university

控制器 studies_controller.rb

def new
  @study = Study.new
  @study.subject = Subject.new
  @study.university = Facility.new
end

def create
  @study = Study.new(params[:study])
  @study.student = current_user

  if @study.save
    flash[:notice] = "Successfully created study."
    redirect_to(:action => 'index')
  else
    render('new')
  end
end

我使用devise 用于身份验证,cancan 用于授权。这就是 current_user 在控制器中可用的原因。

新的研究视图 new.html.erb

<%= form_for @study, :url => { :action => "create" } do |f| %>

  <table summary="Study form fields">

    <%= render :partial => "shared/study_form_fields", :locals =>  { :f => f } %>

    <%= f.fields_for :subject do |builder| %>
      <%= render :partial => "shared/subject_form_fields", :locals =>  { :f => builder } %>
    <% end %>

    <%= f.fields_for :university do |builder| %>
      <%= render :partial => "shared/facility_form_fields", :locals =>  { :f => builder } %>
    <% end %>

  </table>

  <p><%= f.submit "Submit" %></p>

<% end %>

我希望这能为您节省一些时间。我花了很多时间来了解如何设置。

Yeah. It works. A good friend offered his help. This is what we set up.
Please mind that I renamed StudySubject to Subject in the meantime.

The model study.rb

belongs_to :student, :class_name => "User", :foreign_key => "user_id"  
belongs_to :subject  
belongs_to :university, :class_name => "Facility", :foreign_key => "facility_id"  

accepts_nested_attributes_for :subject, :university

The controller studies_controller.rb

def new
  @study = Study.new
  @study.subject = Subject.new
  @study.university = Facility.new
end

def create
  @study = Study.new(params[:study])
  @study.student = current_user

  if @study.save
    flash[:notice] = "Successfully created study."
    redirect_to(:action => 'index')
  else
    render('new')
  end
end

I use devise for authentication and cancan for authorization. That is why current_user is available in the controller.

The new study view new.html.erb

<%= form_for @study, :url => { :action => "create" } do |f| %>

  <table summary="Study form fields">

    <%= render :partial => "shared/study_form_fields", :locals =>  { :f => f } %>

    <%= f.fields_for :subject do |builder| %>
      <%= render :partial => "shared/subject_form_fields", :locals =>  { :f => builder } %>
    <% end %>

    <%= f.fields_for :university do |builder| %>
      <%= render :partial => "shared/facility_form_fields", :locals =>  { :f => builder } %>
    <% end %>

  </table>

  <p><%= f.submit "Submit" %></p>

<% end %>

I hope this will save you some time. I spent a lot of time to realize how things have to be set up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文