我对rails很新,我正在尝试使用Devise(借用者和贷方)设置多个用户类型。我按照本教程Multiple user models with Ruby On Rails and devise to have separate registration routes but one common login route
进行了操作但不断提出错误“未初始化的常量UserRegistrationsController”。
这里是我从基本设计和位置更新的代码(我根据对教程的理解移动/创建了一些代码,但这些可能是错误的):
在:app / controllers / users / registrations_controller.rb
class UserRegistrationsController < Devise::RegistrationsController
def create
build_resource
# customized code begin
# crate a new child instance depending on the given user type
user_type = params[:user][:user_type]
resource.rolable = child_class.new(params[child_class.to_s.underscore.to_sym])
# first check if child instance is valid
# cause if so and the parent instance is valid as well
# it's all being saved at once
valid = resource.valid?
valid = resource.rolable.valid? && valid
# customized code end
if valid && resource.save # customized code
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => redirect_location(resource_name, resource)
else
set_flash_message :notice, :inactive_signed_up, :reason => inactive_reason(resource) if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords(resource)
respond_with_navigational(resource) { render :new }
end
end
end
in:app / controllers / users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@pins = @user.pins.page(params[:page]).per_page(20)
end
end
在:app / models / borrower.rb
class Borrower < ActiveRecord::Base
has_one :user, :as => :rolable
has_many :pins
end
在:app / models / lender.rb
class Lender < ActiveRecord::Base
has_one :user, :as => :rolable
has_many :pins
end
在:app / models / user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:rememberable, :trackable, :validatable #:recoverable,
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :description
# attr_accessible :title, :body
belongs_to :rolable, :polymorphic => true
has_many :pins
end
在:app / views / devise / user_registrations / _borrower_fields.html
<div><%= f.label :label_name %><br />
<%= f.text_field :label_name %></div>
在:app / views / devise / user_registrations / _lender_fields.html
<div><%= f.label :label_name %><br />
<%= f.text_field :label_name %></div>
在:app / views / devise / user_registrations / new.html.erb
<h2>Sign up</h2>
<%
# customized code begin
params[:user][:user_type] ||= 'borrower'
if ["borrower", "lender"].include? params[:user][:user_type].downcase
child_class_name = params[:user][:user_type].downcase.camelize
user_type = params[:user][:user_type].downcase
else
child_class_name = "Borrower"
user_type = "borrower"
end
resource.rolable = child_class_name.constantize.new if resource.rolable.nil?
# customized code end
%>
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: 'form-horizontal'}) do |f| %>
<%= f.error_notification %>
<%= f.input :name %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= f.input :description, label: "Tell Us About Yourself" %>
<% # customized code begin %>
<%= fields_for resource.rolable do |rf| %>
<% render :partial => "#{child_class_name.underscore}_fields", :locals => { :f => rf } %>
<% end %>
<%= hidden_field :user, :user_type, :value => user_type %>
<% # customized code end %>
<div class="form-actions">
<%= f.submit "Sign up", class: "btn btn-primary" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
在:config / locales / routes.rb:
get "users/show"
resources :pins
devise_for :users, :controllers => { :registrations => 'UserRegistrations' }
match 'users/:id' => 'users#show', :as => :user
get 'about' => 'pages#about'
get 'clothing' => 'pages#clothing'
get 'bags' => 'pages#bags'
get 'shoes' => 'pages#shoes'
get 'stylefeed' => 'pages#stylefeed'
get 'accessories' => 'pages#accessories'
get 'designers' => 'pages#designers'
root :to => 'pins#index'
match 'borrower/sign_up' => 'user_registrations#new', :user => { :user_type => 'borrower' }
match 'lender/sign_up' => 'user_registrations#new', :user => { :user_type => 'lender' }
我还在用户表中添加了Rolabe ID和Rolable Type,并生成了注册控制器。
有没有人知道我哪里出错了? 谢谢!
答案 0 :(得分:1)
Rails自动加载无法找到该类的文件。将UserRegistrationsController
重命名为Users::RegistrationsController