导轨形式三种型号和命名空间 [英] Rails form with three models and namespace

查看:188
本文介绍了导轨形式三种型号和命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

敲我的头这一个很长的时间。 On Rails的2.3.2,1.9.1。

Banging my head against this one for a long time. On Rails 2.3.2, Ruby 1.9.1.

尝试使用一种形式创建具有这些关系三个对象:

Trying to use one form to create three objects that have these relations:

class Person
  has_one :goat
end

class Goat
  belongs_to :person
  has_many :kids
end

class Goat::Kid
  belongs_to :goat
end

下面是模式的一个总结:

Here's a summary of the schema:

Person
  first_name
  last_name
Goat
  name
  color
Goat::Kid
  nickname
  age

我想我的#创建操作实例的所有三种型号的新实例,使用指定的关联。然而,尽管看来我的PARAMS哈希被传递给控制器​​,它应该(根据当它炸毁​​浏览器回溯日志)中,山羊::小子对象不收集PARAMS。

I'd like my #create action to instantiate new instances of all three models with the specified associations. However, while it appears that my params hash is being passed to the controller as it should (based on the backtrace logs in the browser when it blows up), the Goat::Kid object is not collecting the params.

IRB(IRB会议只是一个伪重$的什么,我试图完成的,所以如果它不叫 #save p $ psentation!或其他生活必需品它不是真正的意思是正确的。我想所有通过浏览器/ Web表单做到这一点。)

irb (irb session is just a psuedo-representation of what I'm trying to accomplish so if it doesn't call #save! or any other necessities it's not really meant to be correct. I'm trying to do this all through the browser/web form.)

a = Person.new :first_name => 'Leopold', :last_name => 'Bloom'
b = Goat.new :name => 'Billy', :color => 'white'
c = Goat::Kid.new :nickname => 'Jr.', :age => 2

a.goat.kids

>> []

现在,我无法弄清楚如何获取视图的PARAMS传递给每一个对象,并获取控制这些PARAMS保存到数据库中。

Now, I cannot figure out how to get the view to pass the params to each object and to get the controller to save these params to the db.

我的问题:A)是使用的好地方 nested_attributes_for ,如果让我怎么声明一个命名空间? B)有一个更简单,更容易理解的方式做到这一点?

My questions: A) is this a good place to use nested_attributes_for and if so how do I declare that with a namespace? B) is there a much simpler, easier to understand way to do this?

传递到PARAMS三款车型刚刚对我很有挑战性,不管我多么文档阅读,我不能换我的头周围( #form_for #fields_for )。命名空间进一步complexifies这一点。感谢您的帮助!

Passing params to three models has just been very challenging to me and no matter how much documentation I read I can't wrap my head around it (#form_for and #fields_for). The namespace further complexifies this. Thanks for any help!


附录:如果我最终宣告

accepts_nested_attributes_for

什么是用符号参数的命名空间模型的正确方法?

what's the proper way to use the symbol argument for a namespaced model?

accepts_nested_attributes_for :kids, :through => :goats

accepts_nested_attributes_for :goats_kids, :through => :goats

accepts_nested_attributes_for :goats::kids, :through => :goats

我不知道名称空间的模型如何转化为自己的符号标识。谢谢!

I'm not sure how namespaced models translate to their symbol identifiers. Thanks!

推荐答案

好吧,这是我第一次与打accepts_nested_attributes_for ,但有一点玩弄我能得到的东西的工作。

Well, this is my first time playing with accepts_nested_attributes_for, but with a little playing around I was able to get something to work.

首先模型设置:

class Person < ActiveRecord::Base
  has_one :goat
  accepts_nested_attributes_for :goat
end

class Goat < ActiveRecord::Base
  belongs_to :person
  has_many :kids

  accepts_nested_attributes_for :kids
end

class Goat::Kid < ActiveRecord::Base
  belongs_to :goat
end

通过一个简单宁静的控制器:

With a simple restful controller:

ActionController::Routing::Routes.draw do |map|
  map.resources :farm
end

class FarmController < ApplicationController
  def new
  end

  def create
    person = Person.new params[:person]
    person.save
    render :text => person.inspect
  end
end

然后是半复杂的形式:

Then comes the semi-complex form:

接下来,窗体设置:

<% form_for :person, :url => farm_index_path do |p| %>
  <%= p.label :first_name %>: <%= p.text_field :first_name %><br />
  <%= p.label :last_name %>: <%= p.text_field :last_name %><br />
  <% p.fields_for :goat_attributes do |g| %>
    <%= g.label :name %>: <%= g.text_field :name %><br />
    <%= g.label :color %>: <%= g.text_field :color %><br />
    <% g.fields_for 'kids_attributes[]', Goat::Kid.new do |k| %>
      <%= k.label :nickname %>: <%= k.text_field :nickname %><br />
      <%= k.label :age %>: <%= k.text_field :age %><br />
    <% end %>
  <% end %>
  <%= p.submit %>
<% end %>

从看源 accepts_nested_attributes_for ,它看起来像它会创建一个方法,你叫#{attr_name} _attributes = ,所以我需要设置我的 fields_for 来反映(Rails的2.3.3)。接下来,获得的has_many:孩子工作accepts_nested_attributes_for 。在 kids_attributes = 方法一直在寻找对象的数组,所以我需要手动指定的形式排列协会,并告诉 fields_for 使用什么类型的模型。

From looking at the source for accepts_nested_attributes_for, it looks like it will create a method for you called #{attr_name}_attributes=, so I needed to setup my fields_for to reflect that (Rails 2.3.3). Next, getting the has_many :kids working with accepts_nested_attributes_for. The kids_attributes= method was looking for an array of objects, so I needed to specify the array association in the form manually and tell fields_for what type of model to use.

希望这有助于。

这篇关于导轨形式三种型号和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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