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
Sign up as a new user
<%= 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' %><%= f.label :login %>
<%= f.text_field :login %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, 'Confirm Password' %>
<%= f.password_field :password_confirmation %>
<%= f.label :firstname %>
<%= f.text_field :firstname %>
<%= f.label :lastname %>
<%= f.text_field :lastname %>
<%= f.label :phone %>
<%= f.text_field :phone %>
<%= fp.label :photo%>
<%= fp.file_field :photo%>
<%=fa.label :address_1 %>
<%=fa.text_area :Address1,:cols
=> 30,:rows => 3 %>
<%=fa.label :address_2 %>
<%=fa.text_area :Address2,:cols =>
30,:rows => 3 %>
<%=fa.label :City %>
<%=fa.text_field :City %>
<%=fa.label :State %>
<%=fa.text_field :State %>
<%=fa.label :zip_code %>
<%=fa.text_field :ZipCode %>
<%=fa.label :Country %>
<%=fa.text_field :Country %>
<%= submit_tag 'Sign up' %>
<% end -%>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 ([email protected]).”
render :action => ‘new’
end
end