2016-09-21 64 views
1

你好人我在这里有一些挑战与用户的设计认证:我该如何解决这种设计认证挑战?

我有两个模型叫我的应用程序中的颜色和sub_color。 sub_color belongs_to颜色和颜色has_many sub_colors.I已经用适当的数据在数据库中播种

挑战;我希望用户能够在设计form_for中选择这些,当他们注册为集合对象时,sub_color的id也将用于标识特定用户(例如,我可以将所有用户蓝色)。我该如何做到这一点?

这是我已经试过,但它不工作:

%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> 
          <%= f.error_notification %> 

          <div class="form-inputs"> 
          <%= f.input :email, required: true, autofocus: true %> 
          <%= f.input :user_name, required: true %> 
          <%= f.input :password, required: true, hint: ("#  {@minimum_password_length} characters minimum" if @minimum_password_length) %> 
          <%= f.input :password_confirmation, required: true %> 
          <%= f.input :first_name, required: true %> 

    <%= f.label :color_id, "Color" %> <br/> 
    <%= f.collection_select :color_id, Color.order(:name), :id, :name, include_blank: true%> 

    <%= f.label :sub_color_id, "Sub Color" %> <br/> 
    <%= f.grouped_collection_select :sub_color_id, Color.order(:name), :sub_color, :name, :id, :name, include_blank: true%> 

    <div class="form-actions"> 
         <%= f.button :submit, "Sign up" %> 
         </div> 

model for users: 
belongs_to :sub_color 
has_one :color, through: :sub_color 

devise....... 

end 


model for sub_color 

has_many :users 
belongs_to :color 

end 


model for color 

has_many :sub_color 

end  

这是我在网络浏览器中看到

NoMethodError in Devise::Registrations#new 
[undefined method `color_id' for #<User:0xbacc720>] 
+1

你的用户模型中是否有'color_id'和'sub_color_id'? –

+0

不,我没有它,但他们现在在..谢谢,但我仍然有挑战,我用RSB的答案将其添加到模型。 – Dlaw

+0

您的用户,颜色和子颜色模型及其字段的更新问题 –

回答

1

首先错误,你需要添加color_idsub_color_id到用户表。

然后在user.rb belongs_to :sub_color和sub_color.rb has_many :users中定义关联。 color,user.rb belongs_to :color和color.rb has_many :users也一样。

希望有帮助!

+0

我已经完成了上述所有的感谢,但仍显示无方法错误。 – Dlaw

+0

你可以粘贴完整的错误。 – RSB

+0

NoMethodError in Devise :: Registrations#new 显示C:/ Users /...../ new.html.erb其中行#19出现: 未定义方法'color_id'为#<用户:0x851f928> Rails。 root:C:/ Users/c/Documents/Ojojas 应用程序跟踪|框架跟踪|完整跟踪 activemodel(4.2.6)lib/active_model/attribute_methods.rb:433:in'method_missing' actionview(4.2.6)lib/action_view/helpers/tags/base.rb:28:in'public_send' actionview (4.2.6)lib/action_view/helpers/tags/base_rb:28:在'value'中 actionview(4.2.6)lib/action_view/helpers/tags/collection_select.rb:16:在'block in render' – Dlaw