设计:用户所属组织 [英] Devise: User belongs_to organization
问题描述
我正在使用 devise 进行身份验证,并且在注册页面上我有一个用于组织"的文本字段,因此当用户注册时,他们将创建一个组织,并且我希望用户与该组织相关联(用户模型具有组织标识属性).我已经创建了设计视图,并为组织名称添加了一个 fields_for.在我的模型中,我有 User owns_to :organization 和 Organization has_many :users(将有多个用户与组织相关联).我一直在尝试在不修改控制器的情况下尝试做到这一点,但没有走运.请不要建议在不修改控制器的情况下执行此操作,除非您有一个示例应用程序在其中实现了它并且可以指向它.
I am using devise for authentication, and on the Sign Up page I have a text field for 'organization' so when the user signs up, they will create an organization, and I want the user to be associated with that organization (user model has organization_id attribute). I have created the devise views, and added a fields_for for the organization name. In my models I have User belongs_to :organization and Organization has_many :users (there will be more than one user associated to organizations). I have been down every path I could find trying to do this without modifying the controller, but have had no luck. Please don't suggest doing this without modifying the controller unless you have a sample app where you have implemented it that you can point to.
我创建了一个注册控制器,如下所示:覆盖设计注册控制器
I have created a registrations controller as is laid out here: Override devise registrations controller
我只是在控制器中抛出了一些 puts 语句,但我没有看到控制台上显示这些语句,所以看起来我没有进入这个控制器.
I just threw a few puts statements in the controller, and I don't see those being displayed on the console, so it looks like I am not getting to this controller.
我还将我的视图从 app/view/devise/registrations 复制到 app/views/registrations 之后,我的视图似乎来自外太空!我创建的组织字段不再显示,我似乎无法分辨视图是从哪里加载的.
I also copied my views from app/view/devise/registrations to app/views/registrations after which my views seem to come from outer space! The organization field I created no longer is displayed, and I can't seem to tell where the view is loaded from.
抱歉没有更简洁,但我不知道该去哪里.
Sorry for not being more succinct, but I'm not sure where to go with this.
推荐答案
您可以在您的用户模型中使用 accepts_nested_attributes_for
(文档)
You can use accepts_nested_attributes_for
in your User model (documentation)
它应该看起来像:
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
在视图中,您可以使用 Rails 助手或手动创建字段:
In views you could use Rails helper or create field by hand:
<input type="text" name="user[organization_attributes][name]">
<% user = User.new(organization => Organization.new) %>
<%= form_for user do |form| %>
<%= form.fields_for user.organization do |organization_form| %>
<%= organization_form.text_field :name %>
<% end %>
<% end %>
您的设计视图应如下所示:
Your devise view should look like:
<h2>Sign up</h2>
<% resource.organization ||= Organization.new %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<%= f.fields_for resource.organization do |organization_form| %>
<div><%= organization_form.label :name %><br />
<%= organization_form.text_field :name %></div>
<% end %>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render :partial => "devise/shared/links" %>
这篇关于设计:用户所属组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!