Rails devise registration form when having STI

六月ゝ 毕业季﹏ 提交于 2019-12-09 00:06:18

问题


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


回答1:


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




回答2:


I think you're looking for Worker has_one User. 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.



来源:https://stackoverflow.com/questions/4646070/rails-devise-registration-form-when-having-sti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!