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

查看:97
本文介绍了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?

推荐答案

在继续之前,我确实注意到了一个问题-您不需要关联的has_many端的t.references.因此,您在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天全站免登陆