1

我对 Rails 比较陌生,正在尝试使用 Devise(借款人和贷款人)设置多种用户类型。我按照本教程使用 Ruby On Rails 进行了多用户模型,并设计了单独的注册路径,但有一个通用的登录路径

但不断出现错误“未初始化的常量 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

在: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' }

我还在 users 表中添加了一个 Rolabe ID 和一个 Rolable Type 并生成了一个注册控制器。

有谁知道我哪里出错了?谢谢!!

4

1 回答 1

1

Rails 自动加载无法找到该类的文件。重命名UserRegistrationsControllerUsers::RegistrationsController

于 2013-07-17T18:50:05.953 回答