我创建了Devise Users表和Type表。我通过迁移将type_id
列添加到users表中。
以下是home.html.erb
中的链接:
<%= link_to 'Basic Sign up', new_user_registration_path(type: @basic_type), class: 'btn btn-success'%>
<%= link_to 'Pro Sign up', new_user_registration_path(type: @pro_type), class: 'button' %>
以下是pages_controller.rb
class PagesController < ApplicationController
def home
@basic_type = Type.find(1)
@pro_type = Type.find(2)
end
def about
end
end
以下是模型:
type.rb
class Type < ActiveRecord::Base
has_many :users
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
belongs_to :type
end
这是我的类型的迁移文件:
class CreateTypes < ActiveRecord::Migration
def change
create_table :types do |t|
t.string :name
t.timestamps
end
end
end
这是向用户添加类型的迁移文件:
class AddTypeToUser < ActiveRecord::Migration
def change
add_column :users, :type_id, :integer
end
end
schema.rb
中的用户表:
create_table "users", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", limit: 4, default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 255
t.string "last_sign_in_ip", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "type_id", limit: 4
end
这些是我的注册表格: _basic_form.html.erb:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {id: 'basic_type'}) do |f| %>
<%= devise_error_messages! %>
<%= hidden_field_tag 'type', params[:type] %>
<div class="field form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
</div>
<div class="actions form-group">
<%= f.submit "Sign up", class: 'btn btn-success' %>
</div>
<% end %>
_pro_form.html.erb:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= hidden_field_tag 'type', params[:type] %>
<div class="field form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :password %>
<% if @validatable %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
</div>
<div class="field form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
</div>
<div class="actions form-group">
<%= f.submit "Sign up", class: 'btn btn-success', id: 'form-submit-btn' %>
</div>
<% end %>
new.html.erb:
<div class="row">
<div class="col-md-6 col-md-offset-3 text-center">
<% if params[:type] == '2'%>
<h1>Pro Account</h1>
<p>Sign up for the pro account!</p>
<% else %>
<h1>Basic Account</h1>
<p>Sign up for free and get basic access to our community.</p>
<% end %>
</div>
<div class="col-md-6 col-md-offset-3">
<div class="well">
<h2>Sign up</h2>
<% if params[:type] == '2'%>
<%= render "pro_form"%>
<% else %>
<%= render "basic_form"%>
<% end %>
<div class="actions form-group btn btn-default">
<%= render "devise/shared/links" %>
</div>
</div>
</div>
</div>
当我在网址中注册为基本版或专业版时,它会显示:
http://localhost:3000/users/sign_up?type=1
OR
http://localhost:3000/users/sign_up?type=2
但是在type_id
列的数据库中,它显示为nil
。