设计其他用户字段,嵌套模型/属性和自定义路线 [英] Devise with additional user fields, nested models / attributes and custom routes

查看:177
本文介绍了设计其他用户字段,嵌套模型/属性和自定义路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发布了一些有关的问题,但现在我在一个路障。我有这个系统工作得很好,然后我去添加它,现在我打破了它真的很好!



首先我刚刚发现Gist所以这里是我的代码:



https://gist.github.com/ jasper502 / 065a63fde266fac1fbf5



这里的想法是,我有一个用户登录,最终可以通过角色绑定到多个公司。



您通过Devise在自定义/注册路线上注册并填写公司信息等。自定义注册控制器在用户创建过程中创建角色并设置其他几个属性。



当我尝试创建一个新的用户/公司时,额外的用户字段(name_first& name_last)始终无法验证,无论它们是否有效。嵌套公司字段根本无法验证。如果我输入电子邮件和密码字段,则表单只能工作,但只能创建用户记录。



对我来说,似乎没有处理自定义注册控制器,因为似乎不想允许我在sign_up_params中指定的其他参数。



在试用和错误的基础上我尝试过:

  devise_for:users,:skip => [:sessions],:controllers => {:registrations => 'users / registrations',:sessions => 'users / sessions'} 
as:user do
get'/ signin'=> 'devise / sessions#new',:as => :new_user_session
post'/ signin'=> 'devise / sessions#create',:as => :user_session
delete'/ signout'=> 'devise / sessions#destroy',:as => :destroy_user_session
end

我想看看正常的Devise路由是否工作,除了现在验证似乎完全被绕过了。如果我提交一个空白表单,我会收到各种错误,但如果我填写它,它现在可以正常工作。



所以要澄清:




  • 如果我创建自定义注册路由,则附加字段总是验证失败(即使使用正确的数据),也不会创建嵌套对象

  • 如果我使用股票Devise路由表单工作很好IF您输入的数据正确,因为验证中断并允许任何值

  • 再次我确实有这个工作,但不能轻易地扮演这个角色。


解决方案

我将路由从devise / ...切换到user / ...,似乎做了



希望这有助于别人。


I have posted a few related questions on this but now I am at a road block. I had this system working quite well and then I went to add to it and now I broke it real good!

First off I just discovered Gist so here is my code:

https://gist.github.com/jasper502/065a63fde266fac1fbf5

The idea here is that I have a single User login that can eventually be tied to multiple Companies via the Role.

You signup via Devise on the custom /register route and fill out the Company information etc. The custom registration controller creates the Role during the user creation and sets a few other attributes.

When I try to create a new user / company the additional user fields (name_first & name_last) always fail validation regardless if they are in fact valid. The nested Company fields do not validate at all. If I enter the email and password field only the form works but only creates the User record.

To me it seems like the custom registration controller is not being processed at all because it seems to not want to allow my additional params specified in sign_up_params.

On a trial and error basis I tried this:

devise_for :users, :skip => [:sessions], :controllers => { :registrations => 'users/registrations', :sessions => 'users/sessions' }
   as :user do
     get '/signin' => 'devise/sessions#new', :as => :new_user_session
     post '/signin' => 'devise/sessions#create', :as => :user_session
     delete '/signout' => 'devise/sessions#destroy', :as => :destroy_user_session
end

I wanted to see if the normal Devise routes work and they do except that now the validation seems to be bypassed completely. If I submit a blank form I get all kinds of errors but if I fill it out it works fine now. I am getting close but obviously missing something small here.

So to clarify:

  • if I create custom registration routes the additional fields always fail validation (even with correct data) and the nested objects are not created
  • if I use the stock Devise routes the form works great IF you enter in the data correct because the validations break and permit any values
  • again I did have this working but am not in a position to easily role this back

解决方案

I switched my routes to "user/..." from "devise/..." and that seemed to do the trick.

Hope this helps someone else.

这篇关于设计其他用户字段,嵌套模型/属性和自定义路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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