设计:用户属于组织 [英] Devise: User belongs_to organization
问题描述
我已经创建了一个注册控制器在此布局:覆盖设计注册控制器
我刚刚在控制器中投了几个put语句,我没有看到那些在控制台上显示,所以看起来我没有得到这个控制器。
我还将我的意见从app / view / devise / registrationrations复制到app / views / registration,之后我的意见似乎来自外太空!我不再显示我创建的组织字段,我似乎无法告知视图的加载位置。
对不起,更简洁,
您可以使用 acceptable_nested_attributes_for
(文档)
它应该是:
class User< ActiveRecord :: Base
belongs_to:organization
acceptance_nested_attributes_for:organization
end
class Organization< ActiveRecord :: Base
has_many:users
end
在视图中,您可以使用Rails帮助者或手工创建字段:
< input type =textname =user [organization_attributes] [name] >
<%user = User.new(organization => Organization.new)%>
<%= form_for user do | form | %GT;
<%= form.fields_for user.organization do | organization_form | %GT;
<%= organization_form.text_field:name%>
<%end%>
<%end%>
编辑:
您的设计视图应如下所示:
< h2>注册< / h2>
<%resource.organization || = Organization.new%>
<%= form_for(resource,:as => resource_name,:url => registration_path(resource_name))do | f | %GT;
<%= devise_error_messages! %GT;
< div><%= f.label:email%>< br />
<%= f.email_field:email%>< / div>
<%= f.fields_for resource.organization do | organization_form | %GT;
< 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注册%>< / div>
<%end%>
<%= render:partial => devise / shared / links%>
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
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.
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.
You can use accepts_nested_attributes_for
in your User model (documentation)
It should look like:
class User < ActiveRecord::Base
belongs_to :organization
accepts_nested_attributes_for :organization
end
class Organization < ActiveRecord::Base
has_many :users
end
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 %>
EDIT: 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屋!