有 STI 时 Rails 设计注册表 [英] Rails devise registration form when having STI

查看:28
本文介绍了有 STI 时 Rails 设计注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何创建一个工人和一个协会.所以我能够将它们链接在一起.我在用户中有一个类型列.
这是我的表格(http://localhost:3000/workers/sign_up):

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

这是我的模型:

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

我想创建一个注册表单来创建一个用户和一个工作人员.我在 Workers 表中有列,如我在注册表中想要的姓名、年龄和地址.

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.

它是否应该是一个嵌套表单,并且我应该在 worker 和 User 表之间创建关联.

Should it be a nested form and have should i create the association between worker and User table.

最好的问候,Rails 初学者

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'

在您的控制器和视图中,您仍将使用标准的 user_signed_in?

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 时 Rails 设计注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆