Rails 在患有 STI 时设计登记表
我不知道如何创建工人和协会。所以我能够将它们联系在一起。我在用户中有一个类型列。
这是我的表单(http://localhost:3000/workers/sign_up):
<h2>Create Worker</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
<p><%= f.label :kodeord %><br />
<%= f.password_field :password %></p>
<p><%= f.label :bekraeft_kodeord %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Create" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
这是我的模型:
Class Worker < User
devise :database_authenticatable, :registerable
end
Class Company < User
devise :database_authenticatable, :registerable
end
Class User < Appliaction::Base
devise :database_authenticatable, :registerable
end
我想创建一个注册表单来创建用户和工作人员。 我在工人表中有列,如我想要在注册表中显示的姓名、年龄和地址。
它是否应该是一个嵌套表单,并且我应该在工作人员和用户表之间创建关联。
此致, Rails 初学者
I dont know how to create a worker and a association. So i am able to link those together. I have a type colulm in user.
This is my form(http://localhost:3000/workers/sign_up):
<h2>Create Worker</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :email %><br />
<%= f.text_field :email %></p>
<p><%= f.label :kodeord %><br />
<%= f.password_field :password %></p>
<p><%= f.label :bekraeft_kodeord %><br />
<%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Create" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
This is my models:
Class Worker < User
devise :database_authenticatable, :registerable
end
Class Company < User
devise :database_authenticatable, :registerable
end
Class User < Appliaction::Base
devise :database_authenticatable, :registerable
end
I want to create a register form that create a User and a Worker.
I have colums in Workers table as Name, Age and Adress that i want in the registration form.
Should it be a nested form and have should i create the association between worker and User table.
Best regards,
Rails beginner
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的找到了答案,你所要做的就是为你的子类添加路由,然后将用户定向到相关的路由,就像这样
然后你可能会想要为每个子类生成视图
如果没有重大差异您的视图可以只从公司模板中的用户模板渲染文件,就像这样
在您的控制器和视图中您仍将使用标准的 user_signed_in 吗?
希望它能帮助您找到更好的方法,请告诉我
Ok found the answer, what you have to is add the routes for your subclasses, and then dirrect the users to the relevant routes, like such
And then you probably going to want generate the view for each of them
If there are no major differences in your view you can just render the file from the user templates in your companies templates like so
In your controllers and view you will still use the standard user_signed_in?
Hope it helps let me know if you find a better way
我认为您正在寻找
Worker
has_one用户
。我认为你不需要从 User 继承你的 Worker 类。如果您想在创建 Worker 后创建 User,可以在 after_create 回调。I think you're looking for
Worker
has_oneUser
. I don't think you need to inherit your Worker class from User. If you want to create a User after creating a Worker, you can do so in a after_create callback.