Ruby on Rails 中的多态性和形式 [英] Polymorphism and forms in Ruby on Rails

查看:16
本文介绍了Ruby on Rails 中的多态性和形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了很多问题,但是感谢这个很棒的社区,我学到了很多东西.

I've been full of questions lately, but thanks to this awesome community, I'm learning a ton.

我之前获得了多态关联所需的所有帮助,现在我有一个关于使用多态模型处理表单的问题.例如,我有 Phoneable 和 User,所以当我创建表单来注册用户时,我希望能够为用户分配一些电话号码(即:手机、工作、家庭).

I got all the help I needed with polymorphic associations earlier and now I have a question about tackling forms with polymorphic models. For instance I have Phoneable and User, so when I create my form to register a user, I want to be able to assign a few phone numbers to the user (ie: cell, work, home).

class User < ActiveRecord::Base
  has_many :phones, :as => :phoneable
end

class Phone < ActiveRecord::Base
  belongs_to :phoneable, :polymorphic => true
end

class CreateUsers < ActiveRecord::Migration
  t.column :name, :string
  t.references :phoneable, :polymorphic => true
end

class CreatePhones < ActiveRecord::Migration
  t.column :area_code, :integer
  t.column :number, :integer
  t.column :type, :string
  t.references :phoneable, :polymorphic => true
end

现在,当我创建表单时,我感到很困惑.通常我会这样做:

Now when I create my form, I'm getting confused. Normally I would do the following:

- form_for :user, :html => {:multipart => true} do |form|
  %fieldset
    %label{:for => "name"} Name:
    = form.text_field :name
    ##now how do I go about adding a phone number?
    ##typically I'd do this:
    ##%label{:for => "phone"} Phone:
    ##= form.text_field :phone

使用多态,我是否会以同样的方式处理,但使用 fields_for?

Using polymorphism, would I just approach the same way, but use fields_for?

- user_form.fields_for :phone do |phone| %>
  %label{for => "area_code"} Area Code:
  = phone.text_field :area_code
  %label{for => "number"} Number:
  = phone.text_field :number

在这种情况下,这是正确的方法吗?

Is this the correct approach in this instance?

推荐答案

在我们进一步讨论之前,我确实注意到了一个问题 - 你不需要 t.referenceshas_many 关联的一侧.所以你在 create_user 模型中不需要它.它的作用是创建 phonable_idphoneable_type 列,您只需要在多态模型中使用它.

Before we go further, I did notice one issue - you do not need the t.references with the has_many side of the association. So you do not need it in the create_user model. What that does is it creates the phonable_id and the phoneable_type columns, you only need that in the polymorphic model.

使用 fields_for 方法,您正朝着正确的方向前进.但是为了让它工作,你需要告诉模型如何处理这些字段.您可以使用 accepts_nested_attributes_for 类方法来做到这一点.

You are heading down the correct path with the fields_for approach. But in order to get that working, you need to tell the model how to handle those fields. You do that with the accepts_nested_attributes_for class method.

class User < ActiveRecord::Base
  has_many :phones, :as => :phoneable

  accepts_nested_attributes_for :phones
end

还有一件小事,您需要让 fields_for 指向关联的确切名称

and one minor thing, you will need to have the fields_for point to the exact name of the association

- form_for @user do |user_form|
  - user_form.fields_for :phones do |phone|

代替

- form_for @user do |user_form|
  - user_form.fields_for :phone do |phone|

并确保删除您的流浪 %> erb 标记 :)

and make sure you remove your stray %> erb tag :)

更多关于 accepts_nested_attributes_for:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

我希望这会有所帮助!

这篇关于Ruby on Rails 中的多态性和形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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