如何解决这个Devise认证挑战? [英] How do I solve this Devise authentication challenge?
问题描述
你好,我有一些挑战,在这里使用设计认证的用户:
Hello people I have some challenges here with devise authentication for users:
我的应用程序中有两个名为color和sub_color的模型。 sub_color belongs_to颜色和颜色has_many sub_colors.I已经使用适当的数据种子数据库
I have two models called color and sub_color in my application. the sub_color belongs_to color and color has_many sub_colors.I have already seeded the database with the appropriate data
挑战;我希望用户能够在设计form_for中选择它们,当他们注册为一个集合对象,并且sub_color的id也将用于识别特定的用户(例如,我可以排序所有用户的情况蓝色)。如何实现?
The challenge; I want a user to be able to chose these in the devise form_for when they are registering as a collection object and the id of the sub_color will be used for identifying a particular user also(a situation where for instance I can sort all the users which cosed blue color). How do I achieve this please?
这是我已经尝试过的,但它不工作:
This is what I have tried but it is not working:
%= 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
这是我在网络浏览器上看到的错误
This is the error I see on the web browser
NoMethodError in Devise::Registrations#new
[undefined method `color_id' for #<User:0xbacc720>]
推荐答案
首先,您需要向用户表添加 color_id
和 sub_color_id
。
Firstly, You need to add color_id
and sub_color_id
to users table.
然后在user.rb中定义关联 belongs_to:sub_color
和sub_color.rb has_many:users
。同样的, color
,user.rb belongs_to:color
和color.rb has_many:用户
。
Then define association, in user.rb belongs_to :sub_color
and in sub_color.rb has_many :users
. Same goes with color
, user.rb belongs_to :color
and in color.rb has_many :users
.
希望有帮助!
这篇关于如何解决这个Devise认证挑战?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!