Rails 2.3.9 设计1.0.8
我正在尝试将具有管理员角色的用户模型的注册限制为。
不幸的是,我坚持使用Devise 1.0.8和Rails 2.3.9。 我已经阅读了Devise wiki中关于namespacing单独的Users :: Registrations控制器的概述方法,到目前为止,我已经设法进入舞台,因为Users :: Registrations控制器正在呈现一个新的用户表单 - 但是在提交时表单转到原始的Devise Registrations控制器和[:require_no_authentication]过滤器(在Users :: Registrations控制器中跳过)触发并重定向到主页(由于用户已经以管理员身份登录)。
我认为这是路线的一个问题,但我有点难过 - 大多数Google的答案和建议都是针对Devise with Rails 3.有什么想法吗?
Processing Users::RegistrationsController#new (for 158.119.147.40 at 2011-03-28 15:00:15) [GET]
[4;36;1mUser Load (1.6ms)[0m [0;1mSELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1[0m
[4;35;1mRole Load (0.9ms)[0m [0mSELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE ("roles"."name" = E'admin') AND ("roles_users".user_id = 1 ) LIMIT 1[0m
Rendering template within layouts/registrations
Rendering users/registrations/new
[4;36;1mRole Load (0.3ms)[0m [0;1mSELECT * FROM "roles" [0m
[4;35;1mCACHE (0.0ms)[0m [0mSELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles".id = "roles_users".role_id WHERE ("roles"."name" = E'admin') AND ("roles_users".user_id = 1 ) LIMIT 1[0m
Rendered shards/_login_bar (2.6ms)
Rendered shards/_header (3.5ms)
Rendered shards/_menu (1.4ms)
Completed in 66ms (View: 21, DB: 3) | 200 OK [http://158.119.147.40/efoss/users/registrations]
[4;36;1mSQL (0.2ms)[0m [0;1mSET client_min_messages TO 'panic'[0m
[4;35;1mSQL (0.2ms)[0m [0mSET client_min_messages TO 'notice'[0m
Processing RegistrationsController#create (for 158.119.147.40 at 2011-03-28 15:00:35) [POST]
Parameters: {"user"=>{"roles"=>"1", "password_confirmation"=>"zomgapsw0rd", "lname"=>"Ee", "fname"=>"Mr", "password"=>"zomgapsw0rd", "email"=>"mree@notanemail.com"}, "commit"=>"Sign up", "authenticity_token"=>"AViEsObUf5Dadeb0pygJ5BoO8YS9EyURW0vJeBDHiRw="}
[4;36;1mUser Load (1.7ms)[0m [0;1mSELECT * FROM "users" WHERE ("users"."id" = 1) LIMIT 1[0m
Redirected to http://158.119.147.40/efoss/
Filter chain halted as [:require_no_authentication] rendered_or_redirected.
config / routes.rb
map.devise_for :users
map.new_user_registration 'users/registrations', :controller => 'users/registrations', :action => 'new'
#map.connect 'users/registrations', :controller => 'users/registrations', :action => 'create', :conditions => {:method => :post}
控制器/用户/ registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
#prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
skip_before_filter :require_no_authentication
prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
include Devise::Controllers::InternalHelpers
#before_filter :check_permissions, :only => [:new, :create, :cancel]
# GET /resource/sign_up
def new
build_resource
render_with_scope :new
end
# POST /resource
def create
build_resource
if resource.save
set_flash_message :notice, :signed_up
sign_in_and_redirect(resource_name, resource)
else
render_with_scope :new
end
end
# GET /resource/edit
def edit
render_with_scope :edit
end
# PUT /resource
def update
if self.resource.update_with_password(params[resource_name])
set_flash_message :notice, :updated
redirect_to after_sign_in_path_for(self.resource)
else
render_with_scope :edit
end
end
# DELETE /resource
def destroy
self.resource.destroy
set_flash_message :notice, :destroyed
sign_out_and_redirect(self.resource)
end
def check_permissions
authorize! :create, resource
end
end
视图/用户/注册/ new.html.erb
<h2>Sign up</h2>
<% form_for @user do |f| -%>
<%= f.error_messages %>
<p><%= f.label :email %></p>
<p><%= f.text_field :email %></p>
<p><%= f.label :fname, "First name" %></p>
<p><%= f.text_field :fname %></p>
<p><%= f.label :lname, "Last name" %></p>
<p><%= f.text_field :lname %></p>
<p><%= f.label :roles %></p>
<p><%= f.select :roles, Role.all.collect{|r| [r.name, r.id]} %></p>
<p><%= f.label :password %></p>
<p><%= f.password_field :password, {:class => "password_check"} %></p>
<p><%= f.label :password_confirmation %></p>
<p><%= f.password_field :password_confirmation, {:class => "password_check"} %></p>
<p><%= f.submit "Sign up" %></p>
<% end -%>
答案 0 :(得分:0)
最后我最终定义了将表单提交到表单中的url - 不理想并使用了hack - 如果有人可以建议一个更干净的方法来执行此操作我会保存该答案的答案;
的routes.rb
map.devise_for :users
map.new_user_registration '/users/registrations/new', :controller => 'users/registrations', :action => 'new', :conditions => {:method => :get}
map.create_user_registration '/users/registrations/create', :controller => 'users/registrations', :action => 'create', :conditions => {:method => :post}
视图/用户/注册/ new.html.erb
<h2>Sign up</h2>
<% form_for @user, :url => '../../users/registrations/create' do |f| -%>
<%= f.error_messages %>
<p><%= f.label :email %></p>
<p><%= f.text_field :email %></p>
<p><%= f.label :fname, "First name" %></p>
<p><%= f.text_field :fname %></p>
<p><%= f.label :lname, "Last name" %></p>
<p><%= f.text_field :lname %></p>
<p><%= f.label :roles %></p>
<p><%= f.select :roles, Role.all.collect{|r| [r.name, r.id]} %></p>
<p><%= f.label :password %></p>
<p><%= f.password_field :password, {:class => "password_check"} %></p>
<p><%= f.label :password_confirmation %></p>
<p><%= f.password_field :password_confirmation, {:class => "password_check"} %></p>
<p><%= f.submit "Sign up" %></p>
<% end -%>
丑陋的黑客攻击'../../users/registrations'否则表单被路由到'users / registrations / users / registrations' - 如果省略:url修饰符,则表单被提交到默认的Devise注册控制器动作'创建'而非'用户/注册'