Проверка всегда заканчивается на всех полях - PullRequest
1 голос
/ 25 января 2011

Я на Rails 3. У меня есть модель с именем Client, которая имеет name, phone и email.Файл моей модели выглядит следующим образом:

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

Как видите, name, phone и email - все они обязательны.Когда я перехожу к форме, где я должен иметь возможность создать новый Client и отправить его, все три проверки не пройдены, независимо от того, что я положил в поля.Вот мой файл формы:

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

      <ul>
      <% @client.errors.full_messages.each do |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 do |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 %>

Вот мое create действие:

  def create
    @client = Client.new(params[:client])

    respond_to do |format|
      if @client.save
        format.html { redirect_to(@client, :notice => 'Client was successfully 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

Есть идеи, почему это происходит?

1 Ответ

2 голосов
/ 25 января 2011

Это потому, что вы устанавливаете :address_attributes как единственный доступный атрибут. Изменение

attr_accessible :address_attributes

до

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

или не используйте массовое назначение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...