使用 Devise 在 Rails 3.1 中嵌套注册数据 [英] Nested registration data in Rails 3.1 with Devise

查看:47
本文介绍了使用 Devise 在 Rails 3.1 中嵌套注册数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,stackoverflow 社区!

我正在用 Rails 编写一个简单的博客系统,我在身份验证部分使用 Devise.

我进行了基本的电子邮件/密码注册,现在我已经建立了用户表(由设计生成的基本表)和 UserData 表的一对一连接,其中包含用户名、权限、关于等.

这很简单 - 用户表是 100% 设计原版的,并且有一个用户数据.用户数据属于用户.

我尝试扩展由 Devise 生成的注册表单,以便它允许用户输入,不仅是他的电子邮件和密码,还有他想要的用户名.这样,当表单被发送时,如果表单字段的名称是正确的(嵌套哈希的正确结构,例如用户电子邮件的email",以及用户电子邮件的UserData[name]"用户所需的名称),新用户将被自动创建,并且正确的相应 UserData 条目也应该由 Rails 自动创建,对吗?

现在,我遇到了 UserData[name] 字段根本没有出现的大问题...这是我遇到问题的文件...

new.html.erb

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|%><%= devise_error_messages!%><div><%= f.label :email %><br/><%= f.email_field :email %></div>#这是行不通的花絮<%= f.fields_for UserData";做 |ud_f|%><%= ud_f.label 用户名"%><%= ud_f.text_field :name %><%结束%><div><%= f.label :password %><br/><%= f.password_field :password %></div><div><%= f.label :password_confirmation %><br/><%= f.password_field :password_confirmation %></div><div><%= f.submit 注册";%</div><%结束%><%=渲染:部分=>设计/共享/链接"%>

我数小时以来一直在寻找答案,但无济于事.如果我故意拼错UserData"这个词在 fields_for 块中,它打印出该字段.这很奇怪,我不知道是什么让那个领域消失了......

我真的很感激一点点帮助!谢谢你的时间!:)

编辑 1+2:

经过一些混乱之后,我覆盖了设计的默认注册控制器并创建了我自己的.我已经编辑了设计路线以按照 timbrandes 的建议调用我的自定义控制器,但我似乎无法在我的控制器中访问 @user.这是我到目前为止的代码...

class RegistrationsController <设计::注册控制器定义新资源 = build_resource({})resource.UserData = UserData.newresponse_with 资源结尾定义创建logger.debug "\n\n\nIN CREATE\n\n\n\n";logger.debug params.to_json极好的结尾定义更新极好的结尾结尾

解决方案

好吧,很多时间过去了,但我最终通过废弃并重新编写我的大部分 Devise 代码使其工作.老实说,重写并没有那么多,现在效果很好.

对于任何未来的开发人员浏览这个(有点令人困惑)的问题和答案,我能给你的唯一建议是确保编写结构化的表单代码,尤其是对于嵌套资源.这是我当前有效的 registrations_controller.rb:

class RegistrationsController <设计::注册控制器定义新资源 = build_resource({})resource.build_user_dataresponse_with 资源结尾定义创建资源 = build_resource(params[:user])如果(资源.保存)登录(资源名称,资源)response_with 资源,:location =>after_sign_up_path_for(资源)别的渲染:动作=>新的"结尾结尾定义更新极好的结尾结尾

以及注册表单的 .erb:

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|%><%= devise_error_messages!%><div><%= f.label :email %><%= f.email_field :email %></div><%= f.fields_for(:user_data) 做 |udf|%><%= udf.label :name, "所需用户名" %><%= udf.text_field :name %><%= udf.label :roles, "Privileges"%><% 用于 UserData::ROLES 中的角色 %><%= check_box_tag "user[user_data_attributes][roles][]", role, resource.user_data.roles.include?(role) %><%=h role.humanize %<br/><%结束%><%= hidden_​​field_tag "user[user_data_attributes][roles][]" %><%结束%><div><%= f.label :password %><%= f.password_field :password %></div><div><%= f.label :password_confirmation %><%= f.password_field :password_confirmation %></div><div><%= f.submit "注册" %></div><%结束%><%=渲染:部分=>设计/共享/链接"%>

Hello again, stackoverflow community!

I'm working on writing a simple blog system in Rails, and I'm using Devise for the authentication part.

I got the basic email/password registration going, and now I've set up a one-to-one connection of the users table (the basic one generated by devise) and a UserData table, containing things such as username, permissions, about, and so on.

It's pretty easy - the user table is 100% devise vanilla, and has_one UserData. Userdata belongs_to a user.

I tried expanding the sign up form generated by Devise, so that it would allow a user to enter, not just his e-mail and password, but also his desired user name. That way, when the form would be sent, if the names of the form fields would be correct (the right structure of nested hashes, such as "email" for the user's email, and "UserData[name]" for the user's desired name), the new user would be created automatically, and the proper corresponding UserData entry should also be created automatically by Rails, correct?

Right now, I'm having a huge issue with the UserData[name] field not appearing at all... Here's the file I'm having trouble with...

new.html.erb

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

  <div><%= f.label :email %><br />
<%= f.email_field :email %></div>


#This is the tidbit that doesn't work
<%= f.fields_for "UserData" do |ud_f| %>
  <%= ud_f.label "username" %>
  <%= ud_f.text_field :name %>
<% end %>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>

<div><%= f.submit "Sign up" %></div>
<% end %>

<%= render :partial => "devise/shared/links" %>

I have been searching for an answer for hours and hours, but to no avail. If I purposely misspell the word "UserData" in the fields_for block, it prints out the field. This is strange, and I have no idea what's making that field disappear...

I'd really appreciate a little bit of help! Thanks for your time! :)

edit 1+2:

After a few messing around, I've overridden devise's default registration controller and created my own. I've edited the devise route to call my custom controller as timbrandes suggested, but I can't seem to access @user in my controller. Here's the code I have so far...

class RegistrationsController < Devise::RegistrationsController
  def new
   resource = build_resource({})
   resource.UserData = UserData.new
   respond_with resource
  end

  def create
    logger.debug "\n\n\nIN CREATE\n\n\n\n"
    logger.debug params.to_json
    super
  end

  def update
    super
  end
end

解决方案

Well, a lot of time has passed, but I eventually got it working by scrapping and re-doing most of my Devise code. To be honest, it wasn't that much to re-write, and now it works great.

For any future developers browsing this (slightly confusing) question and answer, the only advice I can give you is to make sure to write structured form code, especially for nested resources. This is my current registrations_controller.rb that works:

class RegistrationsController < Devise::RegistrationsController
  def new
    resource = build_resource({})
    resource.build_user_data
    respond_with resource
  end

  def create
    resource = build_resource(params[:user])

    if(resource.save)
      sign_in(resource_name, resource)
      respond_with resource, :location => after_sign_up_path_for(resource)
    else
      render :action => "new"
    end


  end

  def update
    super
  end
end 

And the .erb for the registration form:

<div class="form-box">  

    <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
      <%= devise_error_messages! %>

      <div><%= f.label :email %>
      <%= f.email_field :email %></div>

        <%= f.fields_for(:user_data)  do |udf| %>
          <%= udf.label :name, "Desired user name" %>
          <%= udf.text_field :name %>

          <%= udf.label :roles, "Privileges"%>
          <% for role in UserData::ROLES %>
               <%= check_box_tag "user[user_data_attributes][roles][]" , role, resource.user_data.roles.include?(role) %>
               <%=h role.humanize %><br />
          <% end %>
          <%= hidden_field_tag "user[user_data_attributes][roles][]" %>

        <% end %>

      <div><%= f.label :password %>
      <%= f.password_field :password %></div>

      <div><%= f.label :password_confirmation %>
      <%= f.password_field :password_confirmation %></div>

      <div><%= f.submit "Sign up" %></div>
    <% end %>

    <%= render :partial => "devise/shared/links" %>

</div>

这篇关于使用 Devise 在 Rails 3.1 中嵌套注册数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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