RegistrationsController

RegistrationsController

本文介绍了Rails 4 - devise_for不使用自定义控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails应用程序中使用Devise,我想定制一些它的行为。我已经阅读了 devise_for 的文档。但是我无法使用我的自定义控制器。我的路线如下所示:

  devise_for:用户,控制器:{
会话:users / sessions,
注册:用户/注册
}

在我的 Users :: RegistrationsController< Devise :: RegistrationsController 我尝试过以下操作:

 #POST / resource 
def创建
puts我在这里#永远不会发生!
超级
结束

任何关于我失踪的想法? p>

更新



这是我的文件结构:



更新2



发现了这个问题的根源。我在另一个路径上渲染并呈现我的注册表单,而不是默认的Devise路径。



我在此路径上呈现表单:

  / apps / 1 / edit 

而不是:

  users / sign_up 

如果我在这个路径上注册 users / sign_up ,它的工作原理和我的方法被调用。在我的 / apps / 1 / edit 视图中,我呈现如下形式:

  = renderusers / registrations / modal_form

我的编辑动作为$ apps_controller (我提交注册表单的操作)当前为空:

 #GET / apps / 1 / edit 
def edit
end

相关部分我的 users / registrations / modal_form partial看起来像这样:

  .modal .fade#registrationrations_modal {tabindex:-1,role:'dialog',aria-labelledby
[..]
= renderusers / registrations / new_form
[..]

而我的 users / registrations / new_form 像这样:

  = form_for(resource,as:resource_name,url:registration_path(resource_name))do | f | 
= devise_error_messages!
.form-group
= f.label:name
= f.text_field:name,autofocus:true,class:form-control
.form-group
= f.label:email
= f.email_field:email,class:form-control

.form-group
= f.label:password
- if @minimum_password_length
%em
(#{@ minimum_password_length} characters minimum)
= f.password_field:password,autocomplete:off,class:form-control

.form-group
= f.submit注册,类:btn btn-default

- 如果current_page?(new_user_registration_path)
=渲染devise / shared / links

我不熟悉Devise。任何关于什么和我在哪里丢失的想法?

解决方案

想想我已经解决了。问题出现在我使用错误路由的 form_for 中。而不是使用默认的 registration_path 作为 url 而是使用:

  user_registration_path(resource_name)

愚蠢的错误。进入另一个问题,我现在不断得到: ActionController :: UnknownFormat 注册。但现在至少使用我的自定义控制器。


I use Devise in my Rails app, and I would like to customise some of it's behaviour. I have read the documentation for devise_for. But I can't get it to use my custom controllers. My routs looks like this:

devise_for :users, controllers: {
  sessions: "users/sessions",
  registrations: "users/registrations"
}

In my Users::RegistrationsController < Devise::RegistrationsController I have tried the following:

# POST /resource
def create
  puts "I'm here" # never happens!
  super
end

Any ideas on what I'm missing?

Update

Here is my file structure:

Update 2

Think I might have found the root of the issue. I render and present my signup form in a Bootstrap modal on another route than the default Devise path.

I present the form on this path:

/apps/1/edit

And not:

users/sign_up

If I do sign up on this path users/sign_up it works and my methods get called. In my /apps/1/edit view I present the forms like this:

= render "users/registrations/modal_form"

My edit action for my apps_controller (the action where I present the signup form) is currently empty:

# GET /apps/1/edit
def edit
end

The relevant parts of my users/registrations/modal_form partial looks like this:

.modal.fade#registrations_modal{ tabindex: -1, role: 'dialog', "aria-labelledby"
  [..]
  = render "users/registrations/new_form"
  [..]

And my users/registrations/new_form looks like this:

= form_for(resource, as: resource_name, url: registration_path(resource_name))  do |f|
  = devise_error_messages!
  .form-group
    = f.label :name
    = f.text_field :name, autofocus: true, class: "form-control"
  .form-group
    = f.label :email
    = f.email_field :email, class: "form-control"

  .form-group
    = f.label :password
    - if @minimum_password_length
      %em
        (#{@minimum_password_length} characters minimum)
    = f.password_field :password, autocomplete: "off", class: "form-control"

  .form-group
    = f.submit "Sign up", class: "btn btn-default"

- if current_page?(new_user_registration_path)
  = render "devise/shared/links"

I'm not that familiar with Devise. Any ideas on what and where I'm missing something?

解决方案

Think I solved it. The issue was in my form_for where I used the wrong route. Instead of using the default registration_path as the url I instead use:

user_registration_path(resource_name)

Silly mistake. Ran into another issue, I now keep getting: ActionController::UnknownFormat on signup. But it is at least using my custom controllers now.

这篇关于Rails 4 - devise_for不使用自定义控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 13:34