Ruby on Rails:自定义设计注册控制器,要求创建操作 [英] Ruby on Rails: Custom Devise Registration Controller, Asking For Create Action

查看:135
本文介绍了Ruby on Rails:自定义设计注册控制器,要求创建操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 未知操作

操作创建找不到Devise :: RegistrationsController

是否要求它,因为我有一个自定义注册控制器?如果是这样,这是否意味着我需要复制我不超过这些的所有动作: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb



我的路线:

 

code> 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
#当密码留空时需要设置表单提交
如果params [:user] [:password] .blank? &安培;&安培; PARAMS [:用户] [:password_confirmation] .blank?
params [:user] .delete(:password)
params [:user] .delete(:password_confirmation)
end

@user = User.find current_user.id)
如果@ user.update_attributes(params [:user])
set_flash_message:通知,更新
#登录用户绕过验证,以防其密码更改
sign_in @user,:bypass => true
redirect_to after_update_path_for(@user)
else
呈现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 | %GT; 
...
< div>
<%= button_tag:type => :submit,:class => btn btn-large btn-inversedo%>
注册
<%end%>
< / div>
...
<%end%>


解决方案

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



为了创建自己的自定义注册控制器,对于回溯到原始设计方法的用户,我建议您执行以下操作:

1.创建用户文件夹在控制器文件夹

2.创建有registrations_controller.rb文件,并在其中定义类:

 用户: :注册控制器< Devise :: RegistrationsController 

并覆盖任何操作(编辑和更新)

3.通知routes.rb文件有关更改:

  devise_for:users,:controllers => {注册:'用户/注册'} 


I have a custom registration controller, but I don't want to override a create action from devise. When I try to sign up a user, I get this error:

Unknown action

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

Is it asking for it because I have a custom registration controller? If so, does that mean I need to copy all the actions that I'm not overriding from here: https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb

Or its because there's something wrong with my application?

My routes:

  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

This is my devise registration controller

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

This is the registration form:

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

解决方案

Your controller of registration inherits from the wrong class: DeviseController It is a base class for a registration and has no "create" method, and so does your custom Devise::RegistrationsController class (it has only edit and update methods) - it incurs the error.

In order to create your own custom registration controller for users with fallback to the original devise methods, I suggest you do the following:
1. create "users" folder in controllers folder
2. create there registrations_controller.rb file, and define class there:

Users::RegistrationsController < Devise::RegistrationsController

and override any actions ("edit" and "update")
3. inform the "routes.rb" file about changes:

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

这篇关于Ruby on Rails:自定义设计注册控制器,要求创建操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆