ruby-on-rails-3 rails - 如何刪除Devise路線註冊?





valid_password role (13)


我遇到了同樣的問題,我發現從註冊頁面重定向用戶有點不好。 所以我的解決方案基本上不使用:registrable

我所做的就是創建一個類似於編輯用戶詳細信息的頁面,如下所示:

<%= form_tag(update_user_update_path, method: :post) do %>  
    <br>
    <%= label_tag(:currPassword, 'Current password:') %> <%= password_field_tag(:currPassword) %> <br>
    <%= label_tag(:newPassword, 'New password:') %> <%= password_field_tag(:newPassword) %> <br>
    <%= label_tag(:newPasswordConfirm, 'Confirm new password:') %> <%= password_field_tag(:newPasswordConfirm) %> <br>
    <%= submit_tag('Update') %>
<% end %>

所以這個表單提交到一個新的post端點來更新密碼,如下所示:

  def update
    currPass = params['currPassword']
    newPass1 = params['newPassword']
    newPass2 = params['newPasswordConfirm']
    currentUserParams = Hash.new()
    currentUserParams[:current_password] = currPass
    currentUserParams[:password] = newPass1
    currentUserParams[:password_confirmation] = newPass2
    @result = current_user.update_with_password(currentUserParams)
  end

稍後,您可以在視圖中使用@result來告訴用戶密碼是否已更新。

我在Rails 3應用程序中使用Devise,但在這種情況下,用戶必須由現有用戶創建,該用戶決定他/她將擁有哪些權限。

正因為如此,我想:

  • 刪除用戶註冊的路線
  • 仍然允許用戶在註冊後編輯其個人資料 (更改電子郵件地址和密碼)

我怎樣才能做到這一點?

目前,我通過在devise_for :users之前放置以下內容來有效地移除此路線devise_for :users

match 'users/sign_up' => redirect('/404.html')

這有效,但我想有更好的辦法,對吧?

更新

正如Benoit Garret所說,對我而言,最好的解決方案是跳過創建註冊路線,並創建我想要的註冊路線。

為此,我首先運行rake routes ,然後使用輸出重新創建我想要的rake routes 。 最終的結果是:

devise_for :users, :skip => [:registrations] 
as :user do
  get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
  put 'users' => 'devise/registrations#update', :as => 'user_registration'
end

注意:

  • 我仍然有:registerable在我的User模型中:registerable
  • devise/registrations處理更新電子郵件和密碼
  • 更新其他用戶屬性 - 權限等 - 由不同的控制器處理

實際答案:

刪除默認Devise路徑的路由; 即:

devise_for :users, path_names: {
  sign_up: ''
}



在routes.rb中執行此操作

devise_for :users, :controllers => {:registrations => "registrations"}, :skip => [:registrations]
  as :user do
    get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
    put 'users' => 'devise/registrations#update', :as => 'user_registration'
end

  devise_scope :user do
    get "/sign_in",  :to => "devise/sessions#new"
    get "/sign_up",  :to => "devise/registrations#new"
  end

當您登錄頁面時,您現在會收到一個錯誤,以解決問題。 在app / views / devise / shared / _links.erb中進行更改

<% if  request.path != "/sign_in" %>
    <%- if devise_mapping.registerable? && controller_name != 'registrations' %>
        <%= link_to "Sign up", new_registration_path(resource_name) %><br />
    <% end -%>
<% end %>



這是我走的略有不同的路線。 它使得它不必重寫devise/shared/_links.html.erb視圖。

app/models/user.rb

devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

config/routes.rb

devise_for :users
devise_scope :user do
  put 'users' => 'devise/registrations#update', as: 'user_registration'
  get 'users/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
  delete 'users' => 'devise/registrations#destroy', as: 'registration'
end

之前:

$ rake routes | grep devise
           new_user_session GET    /users/sign_in(.:format)                    devise/sessions#new
               user_session POST   /users/sign_in(.:format)                    devise/sessions#create
       destroy_user_session DELETE /users/sign_out(.:format)                   devise/sessions#destroy
              user_password POST   /users/password(.:format)                   devise/passwords#create
          new_user_password GET    /users/password/new(.:format)               devise/passwords#new
         edit_user_password GET    /users/password/edit(.:format)              devise/passwords#edit
                            PATCH  /users/password(.:format)                   devise/passwords#update
                            PUT    /users/password(.:format)                   devise/passwords#update
   cancel_user_registration GET    /users/cancel(.:format)                     devise/registrations#cancel
          user_registration POST   /users(.:format)                            devise/registrations#create
      new_user_registration GET    /users/sign_up(.:format)                    devise/registrations#new
     edit_user_registration GET    /users/edit(.:format)                       devise/registrations#edit
                            PATCH  /users(.:format)                            devise/registrations#update
                            PUT    /users(.:format)                            devise/registrations#update
                            DELETE /users(.:format)                            devise/registrations#destroy

後:

$ rake routes | grep devise
           new_user_session GET    /users/sign_in(.:format)                    devise/sessions#new
               user_session POST   /users/sign_in(.:format)                    devise/sessions#create
       destroy_user_session DELETE /users/sign_out(.:format)                   devise/sessions#destroy
              user_password POST   /users/password(.:format)                   devise/passwords#create
          new_user_password GET    /users/password/new(.:format)               devise/passwords#new
         edit_user_password GET    /users/password/edit(.:format)              devise/passwords#edit
                            PATCH  /users/password(.:format)                   devise/passwords#update
                            PUT    /users/password(.:format)                   devise/passwords#update
          user_registration PUT    /users(.:format)                            devise/registrations#update
     edit_user_registration GET    /users/edit(.:format)                       devise/registrations#edit
               registration DELETE /users(.:format)                            devise/registrations#destroy



我有類似的問題試圖刪除devise_invitable路徑創建新的

之前:

 devise_for :users

耙路線

accept_user_invitation GET    /users/invitation/accept(.:format)           devise/invitations#edit
       user_invitation POST   /users/invitation(.:format)                  devise/invitations#create
   new_user_invitation GET    /users/invitation/new(.:format)              devise/invitations#new
                       PUT    /users/invitation(.:format)                  devise/invitations#update

devise_for :users , :skip => 'invitation'
devise_scope :user do
  get "/users/invitation/accept", :to => "devise/invitations#edit",   :as => 'accept_user_invitation'
  put "/users/invitation",        :to => "devise/invitations#update", :as => nil
end

耙路線

accept_user_invitation GET    /users/invitation/accept(.:format)                 devise/invitations#edit
                       PUT    /users/invitation(.:format)                        devise/invitations#update

注意1設計範圍https://github.com/plataformatec/devise#configuring-routes

注2我將它應用於devise_invitable,但它可以與任何設計*功能一起使用

重要說明:請參閱devise_scope在用戶而不是用戶 ? 這是正確的,小心這個! 它可以導致很多痛苦給你這個問題:

Started GET "/users/invitation/accept?invitation_token=xxxxxxx" for 127.0.0.1 
Processing by Devise::InvitationsController#edit as HTML
  Parameters: {"invitation_token"=>"6Fy5CgFHtjWfjsCyr3hG"}
 [Devise] Could not find devise mapping for path "/users/invitation/accept?  invitation_token=6Fy5CgFHtjWfjsCyr3hG".
This may happen for two reasons:

1) You forgot to wrap your route inside the scope block. For example:

  devise_scope :user do
     match "/some/route" => "some_devise_controller"
  end

 2) You are testing a Devise controller bypassing the router.
   If so, you can explicitly tell Devise which mapping to use:

    @request.env["devise.mapping"] = Devise.mappings[:user]



這是一個老問題 - 但我最近解決了同樣的問題,並提出了一個比以下更優雅的解決方案:

devise_for :users, :skip => [:registrations] 
as :user do
  get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
  put 'users' => 'devise/registrations#update', :as => 'user_registration'
end

它給出了命名路由的默認名稱(如cancel_user_registration ),而不會過於冗長。

devise_for :users, skip: [:registrations]

# Recreates the Devise registrations routes
# They act on a singular user (the signed in user)
# Add the actions you want in 'only:'
resource :users,
    only: [:edit, :update, :destroy],
    controller: 'devise/registrations',
    as: :user_registration do
  get 'cancel'
end

用默認設計模塊輸出rake routes

                  Prefix Verb   URI Pattern                    Controller#Action
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
       user_registration PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy



您可以通過將它放在“devise_for”之前來覆蓋“devise_scope”。

devise_scope :user do
  get "/users/sign_up",  :to => "sites#index"
end

devise_for :users

不知道這是否是目前最好的解決方案,但它是我的解決方案,因為它只是重定向到登錄頁面。




你可以修改devise寶石本身。 首先,運行以下命令查找使用的安裝位置:

gem which devise

假設路徑是: /usr/local/lib/ruby/gems/1.9.1/gems/devise-1.4.2/lib/devise

然後去

/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.4.2/lib/devise/lib/devise/rails並在該目錄中編輯routes.rb 。 有一個名為def devise_registration(mapping, controllers) ,您可以修改它以擺脫新的操作。 您也可以完全刪除devise_registration的映射




對於我的情況下的其他人。
隨著devise (3.5.2)
我成功地刪除了註冊路線,但保留了編輯配置文件的路徑,並使用以下代碼。

#routes.rb
devise_for :users, skip: [:registrations]
as :user do
  get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
  put '/users(.:format)' => 'devise/registrations#update', as: 'user_registration'
  patch '/users(.:format)' => 'devise/registrations#update'
end



你可以在你的模型中做到這一點

# typical devise setup in User.rb
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

將其更改為:

devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

注意符號:registerable被刪除

就是這樣,沒有其他要求。 到註冊頁面的所有路線和鏈接也被神奇地移除。




我喜歡@ max的answer ,但是當試圖使用它時,由於devise_mapping為零,我遇到了一個錯誤。

我稍微修改了他的解決方案,似乎解決了這個問題。 它需要將調用包裝到devise_scope resource

devise_for :users, skip: [:registrations]

devise_scope :user do
  resource :users,
           only: [:edit, :update, :destroy],
           controller: 'devise/registrations',
           as: :user_registration do
    get 'cancel'
  end
end

請注意, devise_scope需要單數:userresource需要復數:users




通過改變路線,有一大堆其他問題伴隨而來。 我發現的最簡單的方法是執行以下操作。

ApplicationController < ActionController::Base
  before_action :dont_allow_user_self_registration

  private

  def dont_allow_user_self_registration
    if ['devise/registrations','devise_invitable/registrations'].include?(params[:controller]) && ['new','create'].include?(params[:action])
      redirect_to root_path
    end
  end
end



我發現了另一個類似於這個帖子 ,並希望分享@chrisnicola給出的答案。 在帖子中,他們試圖只在製作過程中阻止用戶註冊。

您也可以修改註冊控制器。 你可以使用這樣的東西:

“app / controllers / registrations_controller.rb”

class RegistrationsController < Devise::RegistrationsController
  def new
    flash[:info] = 'Registrations are not open.'
    redirect_to root_path
  end

  def create
    flash[:info] = 'Registrations are not open.'
    redirect_to root_path
  end
end

這將覆蓋設計的控制器,並改為使用上述方法。 他們添加了flash消息,以防某人以某種方式將其發送到sign_up頁面。 您還應該能夠將重定向更改為您喜歡的任何路徑。

另外在“config / routes.rb”中你可以添加這個:

devise_for :users, :controllers => { :registrations => "registrations" }

如此離開它將允許您使用標准設計編輯您的配置文件。 如果您願意,您仍然可以通過包含來覆蓋編輯配置文件選項

  def update
  end

“app / controllers / registrations_controller.rb”




akka.conf打開記錄器







ruby-on-rails-3 routing devise