7

我有一個自定義註冊控制器,但我不想重寫設計的創建操作。當我嘗試註冊用戶時,出現此錯誤:Ruby on Rails:自定義設計註冊控制器,要求創建動作

Unknown action 

The action 'create' could not be found for Devise::RegistrationsController 

這是因爲我有自定義註冊控制器嗎?如果是這樣,這是否意味着我需要複製所有我沒有從這裏覆蓋的操作:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

或者因爲我的應用程序有問題嗎?

我的路線:

devise_for :user, :controllers => { :registrations => "devise/registrations" }, :skip => [:sessions] do 
    get 'signup' => 'devise/registrations#new', :as => :new_user_registration 
    post 'signup' => 'devise/registrations#create', :as => :user_registration 
    end 

這是我的色器件登記控制器

class Devise::RegistrationsController < DeviseController 

    skip_before_filter :require_no_authentication 

    def edit 
    @user = User.find(current_user.id) 
    @profile = Profile.new 
    end 

    def update 
    # required for settings form to submit when password is left blank 
    if params[:user][:password].blank? && params[:user][:password_confirmation].blank? 
     params[:user].delete(:password) 
     params[:user].delete(:password_confirmation) 
    end 

    @user = User.find(current_user.id) 
    if @user.update_attributes(params[:user]) 
     set_flash_message :notice, :updated 
     # Sign in the user bypassing validation in case his password changed 
     sign_in @user, :bypass => true 
     redirect_to after_update_path_for(@user) 
    else 
     render "edit" 
    end 

    end 


    protected 
    def after_update_path_for(resource) 
     user_path(resource) 
    end 

    def after_sign_up_path_for(resource) 
     user_path(resource) 
    end 

end 

這是登記表:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %> 
... 
    <div> 
    <%= button_tag :type => :submit, :class => "btn btn-large btn-inverse" do %> 
    Sign up 
    <% end %> 
    </div> 
... 
<% end %> 

回答

17

您的註冊控制器從錯誤的類繼承: DeviseController 它是一個註冊的基類和h因爲沒有「創建」方法,所以您的自定義Devise :: RegistrationsController類(它只有編輯和更新方法) - 它會產生錯誤。

爲了創建回退到原來的設計一些方法用戶自己定製的登記控制器,我建議你做到以下幾點:
1.控制器文件夾中創建「用戶」文件夾
2.創建有registrations_controller。 RB文件,並有定義類:

Users::RegistrationsController < Devise::RegistrationsController 

,並覆蓋任何操作( 「編輯」 和 「更新」)
3.通知 「的routes.rb」 文件有關的變化:

devise_for :users, :controllers => { registrations: 'users/registrations' } 
+0

這是有效的。爲什麼它不能被設計成文件夾名稱? – hellomello

+0

實際上,我認爲它可以) 但我通常遵循Rails的命名約定我調試此類應用的痛苦經歷:錯誤往往是非常意外的地方,往往是唯一的原因是,一些命名約定不被採納。 –

+2

@RoaringStones你錯過了's'並且註冊結束。該名稱必須是registrations_controller.rb – Ricbermo

相關問題