9

我有一个自定义注册控制器,但我不想覆盖设计中的创建操作。当我尝试注册用户时,我收到此错误:

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

1 回答 1

18

您的注册控制器继承自错误的类: DeviseController 它是注册的基类,没有“创建”方法,您的自定义 Devise::RegistrationsController 类也是如此(它只有编辑和更新方法) - 它会导致错误。

为了为回退到原始设计方法的用户创建自己的自定义注册控制器,我建议您执行以下操作:
1. 在控制器文件夹中创建“用户”文件夹
2.在那里创建 registrations_controller.rb 文件,并在那里定义类:

Users::RegistrationsController < Devise::RegistrationsController

并覆盖任何操作(“编辑”和“更新”)
3. 通知“routes.rb”文件有关更改:

  devise_for :users, :controllers => { registrations: 'users/registrations' }
于 2013-08-23T03:36:12.573 回答