gpt4 book ai didi

ruby-on-rails - Ruby on Rails : Custom Devise Registration Controller, 请求创建操作

转载 作者:行者123 更新时间:2023-12-02 03:30:53 25 4
gpt4 key购买 nike

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

Unknown action

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

它要求它是因为我有一个自定义注册 Controller 吗?如果是这样,这是否意味着我需要从这里复制我没有覆盖的所有操作: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

这是我的设备注册 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

这是注册表:

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

最佳答案

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

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

Users::RegistrationsController < Devise::RegistrationsController

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

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

关于ruby-on-rails - Ruby on Rails : Custom Devise Registration Controller, 请求创建操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18394001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com