2011-03-13 79 views
1

我拔掉了我的所有头发。没有更多的左... :(无法批量分配受保护的属性:

我使用Spree 0.3.4,在一个扩展中我需要注册一些零售商,所以我指示他们到一个零售商的形式,它有许多属于零售商模式的自定义字段...

所以我试图验证/从一种形式提交所有领域,像这样

myextension /应用/视图/ user_registrations/new.html.erb

<%= form_for (:user, :url => registration_path(@user, :type => "retailer) do |f| %> 
    <%= f.fields_for :retailer do |r| %> 
    <%= r.text_field :name %> 
    <% end %> 
    <%= f.text_field :email %> 
<% end %> 

等等等等

class Retailer < ActiveRecord::Base 

belongs_to :user 

validates :name, 
      :presence => true 

end 

class User < ActiveRecord::Base 

has_one :retailer 
accepts_nested_attributes_for :retailer 

attr_accessible :retailer_attributes 


# theres a whole lot more spree and devise stuff here. not sure worth mentioning 

end 

我还增加了在康康舞能力ability.rb

的问题是零售商精密组件永远不会得到验证和数据永远不会插入到数据库...

我创建了一个空白应用程序,并从一些普通的旧脚手架从头开始尝试这个过程,它工作正常。

任何想法??

回答

1

在你的应用助手,做这样的事情(假设你有红宝石1.9 *为龙头的功能,否则结账轨道返回here):

def setup_user(user) 
    user.tap do |u| 
     u.build_retailer if u.retailer.nil? 
    end 
    end 

那么在你看来它改成这样:

<%= form_for (setup_user(@user), :url => registration_path(@user, :type => "retailer) do |f| %> 

看看是否有效。