【问题标题】:Unknown action: The action 'create' could not be found for RegistrationsController?未知操作:找不到 RegistrationsController 的操作“创建”?
【发布时间】:2012-06-12 18:25:46
【问题描述】:

我决定制作一个 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

这里发生了什么?这是如何解决的?

更新


我将create 操作放在protected 之外,但现在我得到了Missing template registrations/create。删除操作让我回到Unknown action: create

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1 devise


    【解决方案1】:

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

    将您的 create 方法从您的 protected 方法中移出:

    class RegistrationsController < ApplicationController
    
      def create
    
      end
    
      protected
    
      def after_sign_up_path_for(resource)
        redirect_to start_path
      end
    
    end
    

    【讨论】:

    • 当我这样做时,它会给我Missing template registrations/create。这似乎不正确,因为我使用的是 Devise。
    • 您通常没有模板来配合create 操作,所以我猜Devise 没有提供模板。通常,您会在成功创建后重定向到 showindex 操作。
    • 啊,但经过进一步调查,似乎缺少create 操作是症状而不是根本问题。
    【解决方案2】:

    看来问题出在您设置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
    

    请注意,RegistrationsController 继承自 Devise::RegistrationsController 而不是 ApplicationController。这样做是为了让您的自定义控制器继承 Devise 的所有正确行为,包括 create 操作。

    【讨论】:

    • 我什至没有看到继承。我还拿走了redirect_to,因为它给出了双重渲染错误。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 2012-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    相关资源
    最近更新 更多