具有Rails多态关联的表单,用于用户注册 [英] Form with Rails polymorphic association for user sign-up

查看:80
本文介绍了具有Rails多态关联的表单,用于用户注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用多态关联进行用户注册的表单,我遇到了问题.我想显示来自多态关联的多个模型的字段,但不能.

I had problems with forms using polymorphic association for an user sign-up. I want to show fields from multiple models of polymorphic association, but I can't.

模型类:

class User < ActiveRecord::Base
  devise ...

  belongs_to :userable, :polymorphic => true
end

class Advisor < ActiveRecord::Base
  has_one :user, :as => :userable
  attr_accessible :extra
  accepts_nested_attributes_for :user # Idon't know if it is ok
end
class AnotherKingOfUser < ActiveRecord::Base
  #the same..
end

我想为每种特定的User类型创建一个控制器,所以:

I want to make a controller for each specific User type, so:

class AdvisorsController < ApplicationController
  # GET /advisors/new
  # GET /advisors/new.json
  def new
    # Here is where I'm not sure about what I did. I tried all variants buy any works for me.
    @advisor = Advisor.new
    @advisor.build_user

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @advisor }
    end
  end
end

我的观点:

<%= form_for(@advisor) do |f| %>
  <% if @advisor.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@advisor.errors.count, "error") %> prohibited this advisor from being saved:</h2>

      <ul>
      <% @advisor.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

 <% #f.fields_for :user do |ff| %>
 <% f.fields_for :user_attributes, @advisor.user||User.new do |ff| %>

    <div class="field">
      <%= ff.label :email %><br />
      <%= ff.text_field :email %>
    </div>

    <div class="field">
      <%= ff.label :password %><br />
      <%= ff.text_field :password %>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :bio %><br />
    <%= f.text_field :bio %>
  </div>


  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

因此,在我的浏览器中,仅出现生物"字段,而没有出现用户模型(Devise)中的扩展字段.

So in my browser only appears "bio" field, but don't appear extended fields from User model (Devise).

推荐答案

class Advisor < ActiveRecord::Base
  has_one :user, :as => :userable
  attr_accessible :extra, :user_attributes
  accepts_nested_attributes_for :user # its ok
end

class AdvisorsController < ApplicationController

  def new
    @advisor = Advisor.new
    # @advisor.build_user   # unnecessary

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @advisor }
    end
  end
end

# view
<%= form_for(@advisor) do |f| %>
  <%= f.fields_for :user_attributes, @advisor.user||User.new do |ff| %>
    <div class="field">
      <%= ff.label :email %><br />
      <%= ff.text_field :email %>
    </div>
    <div class="field">
      <%= ff.label :password %><br />
      <%= ff.text_field :password %>
    </div>
  <% end %>
    ...
<% end %>

这篇关于具有Rails多态关联的表单,用于用户注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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