ruby on rails - Validation always fails on all fields -


i'm on rails 3. have model called client has name, phone , email. model file looks this:

class client < activerecord::base   belongs_to :salon   belongs_to :address   validates_presence_of :name   validates_presence_of :phone   validates_presence_of :email   accepts_nested_attributes_for :address   attr_accessible :address_attributes end 

as can see, name, phone , email required. when go form i'm supposed able create new client , submit it, 3 validations fail, no matter put in fields. here form file:

<%= form_for(@client) |f| %>   <% if @client.errors.any? %>     <div id="error_explanation">       <h2><%= pluralize(@client.errors.count, "error") %> prohibited client being saved:</h2>        <ul>       <% @client.errors.full_messages.each |msg| %>         <li><%= msg %></li>       <% end %>       </ul>     </div>   <% end %>    <%= f.hidden_field :salon_id, :value => salon.logged_in_salon.id %>   <div class="field">     <%= f.label :name %><br />     <%= f.text_field :name %>   </div>   <div class="field">     <%= f.label :phone %><br />     <%= f.text_field :phone %>   </div>   <div class="field">     <%= f.label :email %><br />     <%= f.text_field :email %>   </div>    <%= f.fields_for :address |address_form| %>     <div class="field">       <%= address_form.label :line1 %><br />       <%= address_form.text_field :line1 %>     </div>     <div class="field">       <%= address_form.label :line2 %><br />       <%= address_form.text_field :line2 %>     </div>     <div class="field">       <%= address_form.label :city %><br />       <%= address_form.text_field :city %>     </div>     <div class="field">       <%= address_form.label :state_id %><br />       <%= select("client[address]", "state_id", state.all.collect {|s| [ s.name, s.id ] }) %>     </div>     <div class="field">       <%= address_form.label :zip %><br />       <%= address_form.text_field :zip %>     </div>   <% end %>    <div class="actions">     <%= f.submit %>   </div> <% end %> 

here's create action:

  def create     @client = client.new(params[:client])      respond_to |format|       if @client.save         format.html { redirect_to(@client, :notice => 'client created.') }         format.xml  { render :xml => @client, :status => :created, :location => @client }       else         format.html { render :action => "new" }         format.xml  { render :xml => @client.errors, :status => :unprocessable_entity }       end     end   end 

any idea why happening?

it's because set :address_attributes accessible attribute. change

attr_accessible :address_attributes 

to

attr_accessible :address_attributes, :name, :phone, :email 

or don't use mass assignment.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -