error using nested attributes

1 view
Skip to first unread message

Sunny Bogawat

unread,
Aug 27, 2010, 12:14:24 AM8/27/10
to rubyonra...@googlegroups.com
I have following models

class User < ActiveRecord::Base

belongs_to :user_type
has_many :addresses, :as =>:addressee, :dependent => :destroy
has_many :photos,:as => :asset,:dependent => :destroy

accepts_nested_attributes_for :addresses, :allow_destroy => true
accepts_nested_attributes_for :photos, :allow_destroy => true
end

class Photo < ActiveRecord::Base

belongs_to :asset, :polymorphic => true

validates_attachment_presence :photo unless :photo
validates_attachment_content_type :photo, :content_type =>
['image/jpeg', 'image/jpg', 'image/gif', 'image/png', 'image/bmp']
unless :photo
attr_accessor :photo

# Paperclip
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb
=> "100x100>" }
end

class Address < ActiveRecord::Base

belongs_to :addressee, :polymorphic => true

validates_presence_of :Address1, :maximum => 50
validates_presence_of :City
validates_presence_of :State
validates_presence_of :ZipCode
validates_presence_of :Country
end

i have one form having combination of all this field if i hit submit
button without adding any data it show all validation from users
addresses model but not showing photo model validations & hiding photo
field from UI
not getting how this is happening? i am using nested attributes

here is my form

<div align="center">
<h1>Sign up as a new user</h1>
<%= error_messages_for :user %>
<% @user.password = @user.password_confirmation = nil %>
<% form_for @user,:url => users_path,:html => { :multipart => true } do
|f| -%>
<%= f.hidden_field :user_type_id,:value=>'2' %>
<p><%= f.label :login %><br/><%= f.text_field :login %></p>
<p><%= f.label :email %><br/><%= f.text_field :email %></p>
<p><%= f.label :password %><br/><%= f.password_field :password %></p>
<p><%= f.label :password_confirmation, 'Confirm Password' %><br/>
<%= f.password_field :password_confirmation %></p>
<p><%= f.label :firstname %><br/><%= f.text_field :firstname %></p>
<p><%= f.label :lastname %><br/><%= f.text_field :lastname %></p>
<p><%= f.label :phone %><br/><%= f.text_field :phone %></p>
<% f.fields_for :photos do |fp| %>
<p><%= fp.label :photo%><br/><%= fp.file_field :photo%></p>
<% end %>
<% f.fields_for :addresses do |fa| %>
<p><%=fa.label :address_1 %><br/><%=fa.text_area :Address1,:cols
=> 30,:rows => 3 %></p>
<p><%=fa.label :address_2 %><br/><%=fa.text_area :Address2,:cols =>
30,:rows => 3 %></p>
<p><%=fa.label :City %><br/><%=fa.text_field :City %></p>
<p><%=fa.label :State %><br/><%=fa.text_field :State %></p>
<p><%=fa.label :zip_code %><br/><%=fa.text_field :ZipCode %></p>
<p><%=fa.label :Country %><br/><%=fa.text_field :Country %></p>
<% end %>

<p><%= submit_tag 'Sign up' %></p>
<% end -%>
</div>

in controller

def new
@user = User.new
@user.photos.build
@user.addresses.build
end

#Create new user.
def create
logout_keeping_session!
@user = User.new(params[:user])
if @user.save
redirect_back_or_default('/')
flash[:notice] = "Thanks for signing up! We're sending you an
email with your activation code."
else
flash[:error] = "Sorry!! There was an error while setting up the
account. Please try again, or contact our admin (ad...@makemyhome.com)."
render :action => 'new'
end
end
--
Posted via http://www.ruby-forum.com/.

Sunny Bogawat

unread,
Oct 6, 2010, 3:00:33 AM10/6/10
to rubyonra...@googlegroups.com
anybody have any idea on it?


Sunny Bogawat wrote:
> I have following models
>
> class User < ActiveRecord::Base
>
> belongs_to :user_type
> has_many :addresses, :as =>:addressee, :dependent => :destroy
> has_many :photos,:as => :asset,:dependent => :destroy
>
> accepts_nested_attributes_for :addresses, :allow_destroy => true
> accepts_nested_attributes_for :photos, :allow_destroy => true
> end
>
> class Photo < ActiveRecord::Base
>

Reply all
Reply to author
Forward
0 new messages