Rails 3.2从子视图创建父模型 [英] Rails 3.2 Create Parent Model from Child View

查看:103
本文介绍了Rails 3.2从子视图创建父模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解如何执行此操作.我有两个模型,一个项目和一个课程.

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

Projects#new页面(子对象)上,我要键入新的course的名称并让其创建父对象.

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.

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

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

我正在使用haml,因此应该显示=,但是的字段甚至都不会显示在页面或生成的html中.关于这是为什么的任何线索? (提交"按钮会显示)

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)

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

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

推荐答案

您应该使用表单生成器来构建对象,而不是单独使用fields_for.

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

您有这个:

= fields_for :course do |builder|

您应该在哪里:

= f.fields_for :course do |builder|

区别在于,通过在初始表单生成器上调用它,Rails将检查初始form_for调用中对象上是否存在course_attributes=方法(在本例中,该方法为),如果有的话,它将在此表单内将字段定义为course_attributes.

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.

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

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.

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

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

这篇关于Rails 3.2从子视图创建父模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆