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

问题描述

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

未知动作无法为 Devise::RegistrationsController 找到操作创建"

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

还是因为我的应用程序有问题?

我的路线:

 devise_for :user, :controllers =>{:注册=>"设计/注册" }, :skip =>[:sessions] 做获取注册"=>'设计/注册#new', :as =>:new_user_registration发布注册"=>'设计/注册#create', :as =>:用户注册结尾

这是我的设计注册控制器

class Devise::RegistrationsController <设计控制器skip_before_filter :require_no_authentication定义编辑@user = User.find(current_user.id)@profile = Profile.new结尾定义更新# 当密码留空时需要提交设置表单如果 params[:user][:password].blank?&&params[:user][:password_confirmation].blank?params[:user].delete(:password)params[:user].delete(:password_confirmation)结尾@user = User.find(current_user.id)如果@user.update_attributes(params[:user])set_flash_message :notice, :updated# 绕过验证登录用户以防他的密码更改sign_in @user, :bypass =>真的redirect_to after_update_path_for(@user)别的渲染编辑"结尾结尾受保护def after_update_path_for(资源)用户路径(资源)结尾def after_sign_up_path_for(资源)用户路径(资源)结尾结尾

这是注册表:

 resource_name, :url => registration_path(resource_name)) do |f|%>...<div>:提交, :class =>"btn btn-large btn-inverse" do %>注册<%结束%>

...<%结束%>

解决方案

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

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

Users::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: DeviseControllerIt 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:自定义设计注册控制器,请求创建操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 23:21