Ruby on Rails:自定义设计注册控制器,请求创建操作

hel*_*llo 8 ruby-on-rails devise ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

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

Unknown action

The action 'create' could not be found for Devise::RegistrationsController
Run Code Online (Sandbox Code Playgroud)

它是否要求它,因为我有一个自定义注册控制器?如果是这样,这是否意味着我需要复制我不会从这里覆盖的所有操作: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
Run Code Online (Sandbox Code Playgroud)

这是我的设计注册控制器

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
Run Code Online (Sandbox Code Playgroud)

这是注册表格:

<%= 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 %>
Run Code Online (Sandbox Code Playgroud)

Roa*_*nes 17

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

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

Users::RegistrationsController < Devise::RegistrationsController
Run Code Online (Sandbox Code Playgroud)

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

  devise_for :users, :controllers => { registrations: 'users/registrations' }
Run Code Online (Sandbox Code Playgroud)

  • @RoaringStones你错过了's'和注册结束.名称必须是registrations_controller.rb (2认同)