轨道具有STI的注册表格 [英] Rails devise registration form when having STI
问题描述
我不知道如何创建一个工作者和一个关联。所以我可以把它们连在一起。我有一个类型colulm在用户。
这是我的表单(http:// localhost:3000 / workers / sign_up):
< h2>创建工作者< / h2>
<%= form_for(resource,:as => resource_name,:url => registration_path(resource_name))do | f | %GT;
<%= devise_error_messages! %GT;
< p><%= f.label:email%>< br />
<%= f.text_field:email%>< / p>
< p><%= f.label:kodeord%>< br />
<%= f.password_field:password%>< / p>
< p><%= f.label:bekraeft_kodord%>< br />
<%= f.password_field:password_confirmation%>< / p>
< p><%= f.submit创建%>< / p>
<%end%>
<%= render:partial => devise / shared / links%>
这是我的型号:
类工作者<用户
devise:database_authenticatable,:可注册
end
Class公司<用户
devise:database_authenticatable,:可注册
end
类用户< Appliaction :: Base
devise:database_authenticatable,:可注册
end
我想创建一个创建用户和工作者的注册表单。
我在工作表中有列表中的列表,名称,年龄和地址,我想在注册表单。
如果它是一个嵌套的形式,我应该创建工作者和用户表之间的关联。
最佳答案,
Rails初学者
Ok找到答案,你必须添加您的子类的路线,然后将用户定向到相关路线,像这样
devise_for:company
devise_for:workers
然后你可能要生成查看每个
脚本/ rails生成设计:视图公司
如果您的观点没有重大差异,您可以从公司模板中的用户模板中渲染文件,如此
render:file => '用户/注册/新'
在您的控制器和视图中,您仍然会使用标准的user_signed_in? / p>
希望有帮助让我知道如果你找到更好的方法
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
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
devise_for :companies
devise_for :workers
And then you probably going to want generate the view for each of them
script/rails generate devise:views companies
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
render :file => 'users/registrations/new'
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
这篇关于轨道具有STI的注册表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!