将字段添加到多态设计用户轨道4

时间:2016-06-02 00:43:51

标签: ruby-on-rails-4 devise polymorphism

我有一个rails应用程序,使用设计用户。我有两种类型的用户共享一些字段,然后有自己的。我为这两种类型制作了多态模型。当我向基础设备用户添加字段时,添加新用户的表单似乎没有识别新字段。

设计用户

class User < ActiveRecord::Base

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  belongs_to :meta, polymorphic: true
end

多态艺术家模型

class Artist < ActiveRecord::Base
    has_one :user, as: :meta, dependent: :destroy
    has_and_belongs_to_many :artist_expertises
    accepts_nested_attributes_for :user

    has_many :connections
    has_many :teachers, through: :connections
end

覆盖设计注册

class RegistrationsController < Devise::RegistrationsController
    private

    def sign_up_params
        params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
    end

    def account_update_params
        params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
    end

end

路由

Rails.application.routes.draw do

...
  resources :artists
  resources :teachers
  devise_for :users, :controllers => { registrations: 'registrations' }
...
end

然后,对于艺术家的新页面中使用的以下_form,默认情况下来自设计的电子邮件和密码字段。我添加到users表错误的:first_name字段的下一个user_fields.label(未定义的局部变量或方法`user_fields&#39; for#&lt;#:0x007f9c81b3a108&gt; 你的意思是? number_field)

views / artist / _form.html.erb(在新视图中呈现)

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

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

    <%= f.fields_for :user do |user_fields| %>
    <div class="field">
      <%= user_fields.label :email %>
      <%= user_fields.text_field :email %>
    </div>
    <div class="field">
      <%= user_fields.label :password %>
      <%= user_fields.text_field :password %>
    </div>
    <% end %> 

    <div class="field">
      <%= user_fields.label :first_name %><br />
      <%= user_fields.text_field :first_name %>
    </div>

    <div class="field">
      <%= user_fields.label :last_name %><br />
      <%= user_fields.text_field :last_name %>
    </div>

    <div class="field">
      <%= f.label :inspiration %><br>
      <%= f.text_area :inspiration %>
    </div>

    <div class="field">
      <%= f.label "Area of Expertise" %><br />
      <%= f.collection_check_boxes :artist_expertise_ids, ArtistExpertise.all, :id, :name do |b| %>
      <div class="collection-check-box">
        <%= b.check_box %>
        <%= b.label %>
      </div>
      <% end %>
    </div>


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

1 个答案:

答案 0 :(得分:0)

谢谢,我是一个叮咚。有&lt;%end%&gt;在我添加用户字段之前。 谢谢你帮忙呀砰砰的砰砰声。