簡體   English   中英

設計注冊:: Routes rails 2.3.9

[英]Devise registrations :: Routes rails 2.3.9

Rails 2.3.9設計1.0.8

我試圖將Devise用戶模型的注冊限制為具有管理員角色的用戶。

不幸的是,我堅持使用Devise 1.0.8和Rails 2.3.9。 我已經閱讀了Devise Wiki中概述的有關命名間隔單獨的Users :: Registrations控制器的方法,到目前為止,我設法達到了階段:Users :: Registrations控制器正在呈現新的用戶表單-但是在提交時表單轉到原始的Devise Registrations控制器,然后觸發[:require_no_authentication]過濾器(在Users :: Registrations控制器中跳過了該過濾器)並重定向到主頁(由於User已經以管理員身份登錄)。

我認為這是路線問題,但我有些困惑-Google的大多數答案和建議都適用於Devise with Rails3。有什么想法嗎?

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

最后,我最終定義了將表單提交給表單本身的網址-不理想,並且使用了一些技巧-如果有人可以建議一種更清潔的方式,則我將為該答案留點號;

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 Registrations控制器操作“創建”而不是“用戶/注冊”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM