jeudi 4 septembre 2014

Rails 4 - Polymorphic Association - Nested Attributes not showing in form


Vote count:

0




I am building a login system that has two users, buyer and seller, in Rails 4.0.4.


For auth I am using the Devise gem: http://ift.tt/LkuK7q


To create a new buyer I use the route buyer/new. However, the fields for the user do not show in the view. I also using debug to show @buyer.user in the view and it has been created. But when I call f.fields_for @buyer.user do |u| the loop is never entered.


Any ideas of why this is? Also, the polymorphic associations seem to be working in the rails console.


Buyer Controller:



# GET /buyers/new
def new
@buyer = Buyer.new
@buyer.build_user
end


Buyer Model



class Buyer < ActiveRecord::Base
has_one :user, as: :role
accepts_nested_attributes_for :user
end


Buyer/new View



<%= form_for(@buyer) do |f| %>
....
<div class="field">
<%= debug(@buyer.user) %>
<% f.fields_for @buyer.user do |u| %>
<%= u.text_field :email %>
<% end %>
</div>


User Model



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

belongs_to :role, polymorphic: true


asked 1 min ago

John

183






Rails 4 - Polymorphic Association - Nested Attributes not showing in form

Aucun commentaire:

Enregistrer un commentaire