Rails 3.2 从子视图创建父模型

发布于 2024-12-28 11:21:18 字数 1311 浏览 1 评论 0原文

我很难理解如何做到这一点。我有两个模型、一个项目和一个课程。

#project.rb
belongs_to :course
attr_accessible :course_id, :course
accepts_nested_attributes_for :course, reject_if: lambda { |a| a[:course_id] == 0 }

#course.rb
has_many :projects

Projects#new 页面(子对象)上,我想输入新course 的名称并让它创建父对象。

这是我在视图中的尝试,但它似乎无法正常工作。

= form_for [@user, @project] do |f|

  # Other fields

  = fields_for :course do |builder|
    = builder.label :name, 'Course Name'
    = builder.text_field :name

  = f.submit

稍后我将使用此父对象来创建更多项目,但现在,我们假设它不存在。

更新1 我已将 fields_for 修改为(根据 Ryan 的要求):

= form_for [@user, @project] do |f|

  # Other fields

  = f.fields_for :course do |builder|
    = builder.label :name, 'Course Name'
    = builder.text_field :name

  = f.submit

我正在使用 haml,因此应该显示 = ,但 for 的字段甚至不会显示在页面上或中生成的html。有什么线索可以解释为什么吗? (提交按钮确实显示)

更新 2 我找到了一个潜在的解决方案,但我不确定这是否是解决此问题的正确方法。在控制器中,我需要构建一个课程以便 fields_for 显示。

# ProjectsController
def new
  @project  = @user.projects.new
  @project.build_course
end

# project.rb
attr_accessible :course_attributes
# So yes, I now see what you were talking about, regarding the course_attributes

I'm having a difficult time understanding how to do this. I have two models, a project, and a course.

#project.rb
belongs_to :course
attr_accessible :course_id, :course
accepts_nested_attributes_for :course, reject_if: lambda { |a| a[:course_id] == 0 }

#course.rb
has_many :projects

On the Projects#new page (child object), I want to type in the name of a new course and have it create the parent object.

Here's my attempt in the view, but it doesn't seem to be working correctly.

= form_for [@user, @project] do |f|

  # Other fields

  = fields_for :course do |builder|
    = builder.label :name, 'Course Name'
    = builder.text_field :name

  = f.submit

I'll be using this parent object later to create more projects, but for now, let's assume it doesn't exist.

UPDATE 1
I've modified my fields_for to be (as per Ryan's request):

= form_for [@user, @project] do |f|

  # Other fields

  = f.fields_for :course do |builder|
    = builder.label :name, 'Course Name'
    = builder.text_field :name

  = f.submit

I'm using haml, so the = should be displaying, but the fields for does not even show up on the page, or in the generated html. Any clue as to why that is? (The submit button does display)

UPDATE 2
I've found a potential solution, but I'm not sure if it's the correct way of approaching this. In the controller, I need to build a course for the fields_for to show up.

# ProjectsController
def new
  @project  = @user.projects.new
  @project.build_course
end

# project.rb
attr_accessible :course_attributes
# So yes, I now see what you were talking about, regarding the course_attributes

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

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

发布评论

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

评论(2

別甾虛僞 2025-01-04 11:21:19

您应该使用表单构建器来构建您的对象,而不仅仅是 fields_for 本身。

你有这个:

= fields_for :course do |builder|

你应该有这个:

= f.fields_for :course do |builder|

区别在于,通过在初始表单构建器上调用它,Rails 将检查初始 对象上是否有 course_attributes= 方法>form_for 调用(在本例中为 @project),如果存在,那么它将将此表单内的字段定义为 course_attributes

继续检查此更改之前和之后的表格,只是为了看看。我会等待。

这是通过模型中的 accepts_nested_attributes_for 调用实现的。正是该方法定义了允许嵌套属性发挥作用的 course_attributes= 方法。创建项目后,它还应该创建课程。

另外,无需将 course_id 设置为可访问属性,因为您的表单不会对其进行设置。

You should be using the form builder to build out your object, rather than just fields_for by itself.

You have this:

= fields_for :course do |builder|

Where you should have this:

= f.fields_for :course do |builder|

The difference is that by calling it on the initial form builder, Rails will check to see if there is a course_attributes= method on the object from the initial form_for call (in this case, that'd be @project) and if there is then it'll define the fields inside this form as being course_attributes.

Go ahead and inspect the form before and after this change, just to see. I'll wait.

This is made possible by the accepts_nested_attributes_for call in your model. It's this method that defines the course_attributes= method that allows for the nested attributes to work. Once you create the project, it should then also create the course.

Also, no need to make course_id an accessible attribute, as your form's not going to be setting that.

人│生佛魔见 2025-01-04 11:21:19

我使用了 gem 'dynamic_form'。
coach_list(id, coach_name) 表包含院系名称。

<%= f.select :faculty, options_from_collection_for_select(@faculty_list, 'id', 'faculty_name', @faculty.faculty.to_i) %>

I used gem 'dynamic_form'.
faculty_list(id, faculty_name) table contains names of faculties.

<%= f.select :faculty, options_from_collection_for_select(@faculty_list, 'id', 'faculty_name', @faculty.faculty.to_i) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文