设计:用户属于组织

发布于 2024-12-13 12:00:50 字数 668 浏览 0 评论 0原文

我正在使用设备进行身份验证,在“注册”页面上,我有一个“组织”文本字段,因此当用户注册时,他们将创建一个组织,并且我希望用户与该组织关联(用户模型有Organization_id 属性)。我已经创建了设计视图,并添加了 fields_for 作为组织名称。在我的模型中,我有 User own_to :organization 和 Organization has_many :users (将有多个用户与组织关联)。我已经尝试了所有我能找到的方法来尝试在不修改控制器的情况下做到这一点,但没有运气。请不要建议在不修改控制器的情况下执行此操作,除非您有一个可以指向的已实现该控制器的示例应用程序。

我已经创建了一个注册控制器,如下所示: 覆盖设备注册控制器

我刚刚抛出了一个控制器中很少有 put 语句,而且我没有看到这些语句显示在控制台上,所以看起来我没有到达该控制器。

我还将我的视图从 app/view/devise/registrations 复制到了 app/views/registrations,之后我的视图似乎来自外太空!我创建的组织字段不再显示,而且我似乎无法判断视图是从哪里加载的。

很抱歉没有更简洁,但我不知道该去哪里。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮倦 2024-12-20 12:00:50

您可以在用户模型中使用 accepts_nested_attributes_for文档)

它应该看起来像:

class User < ActiveRecord::Base
  belongs_to :organization
  accepts_nested_attributes_for :organization
end

class Organization < ActiveRecord::Base
  has_many :users
end

在视图中,您可以使用Rails助手或手动创建字段:

<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 %>

编辑:
您的设备视图应如下所示:

<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" %>

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" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文