2

我決定製作一個RegistrationsController,這樣我就可以將用戶註冊到特定的頁面。唯一的問題是,用戶甚至不會創建,因爲我得到的錯誤:未知的操作:無法爲RegistrationsController找到「創建」操作?

Started POST "/users" for 127.0.0.1 at 2012-06-12 14:01:22 -0400 

AbstractController::ActionNotFound (The action 'create' could not be found for R 
egistrationsController): 

我的路線和控制器:

devise_for :users, :controllers => { :registrations => "registrations" } 
    devise_scope :user do 
    get "/sign_up" => "devise/registrations#new" 
    get "/login" => "devise/sessions#new" 
    get "/log_out" => "devise/sessions#destroy" 
    get "/account_settings" => "devise/registrations#edit" 
    get "/forgot_password" => "devise/passwords#new", :as => :new_user_password 
    get 'users', :to => 'pages#home', :as => :user_root 
    end 

class RegistrationsController < ApplicationController 
    protected 

    def after_sign_up_path_for(resource) 
    redirect_to start_path 
    end 

    def create # tried it with this but no luck. 

    end 
end 

這是怎麼回事?這是如何修復的?

UPDATE


我把create行動protected之外,但現在我得到一個Missing template registrations/create。刪除操作會將我帶回Unknown action: create

回答

4

看起來,問題是你已經設置了RegistrationsController的方式。如果您在the Devise wiki page explaining how to do this看一看,你會看到下面的例子:

class RegistrationsController < Devise::RegistrationsController 
    protected 

    def after_sign_up_path_for(resource) 
    '/an/example/path' 
    end 
end 

注意,RegistrationsControllerDevise::RegistrationsController而不是ApplicationController繼承。這樣做是爲了讓您的自定義控制器繼承Devise的所有正確行爲,包括create操作。

+0

我甚至沒有看到這種繼承。我也拿走了'redirect_to',因爲這給了雙重渲染錯誤。謝謝。 – LearningRoR

6

您的create方法是protected,這意味着它不能被路由到。

移動你的create方法出你protected方法:

class RegistrationsController < ApplicationController 

    def create 

    end 

    protected 

    def after_sign_up_path_for(resource) 
    redirect_to start_path 
    end 

end 
+0

當我這樣做,它給了我'缺少模板註冊/創建'。這似乎不正確,因爲我使用Devise。 – LearningRoR

+0

你通常沒有一個模板來跟着「創建」動作,所以我猜測Devise沒有提供。通常,創建成功後,您將重定向到「show」或「index」操作。 – Emily

+0

嗯,但是稍微調查一下,看起來缺乏「創造」行動是一個症狀,而不是根本問題。 – Emily

相關問題