Rails多态模型。如何在视图上显示表单验证错误?

时间:2010-07-08 16:40:45

标签: ruby-on-rails forms polymorphic-associations

我的多态关联工作正常,但如果我有模型验证,那么错误就不会出现了。

在我的控制器中我有:

def create
    @locatable = find_locatable
    @geographic_location = @locatable.geographic_locations.build(params[:geographic_location])



    if @geographic_location.save
      flash[:notice] = t('migos.controller.geo_location_saved')
      redirect_to([@locatable, :geographic_locations])
    else
      flash[:error] = t('migos.controller.geo_location_error')
      render :action => 'new'
    end
  end

在模型中:

class GeographicLocation < ActiveRecord::Base

  belongs_to :locatable, :polymorphic => true

  validates_presence_of :city, :message => "Falta Cidade"
  validates_presence_of :location

class User < ActiveRecord::Base
 has_many :geographic_locations, :as => :locatable

非常标准的东西。但事实是,当render:action =&gt;发生'编辑',它会转到右页,但错误不会显示。

我的new.html.erb视图:

<% semantic_form_for [@locatable, GeographicLocation.new] do |f| %>
    <% f.inputs :id => "geo_location" do %>
        <%= f.input :street, :label =>"Rua" %>
                <%= f.input :location, :label =>"Localidade" %>
        <%= f.input :city, :label =>"Cidade" %>
          <div class="clear geo"></div>
                <%= f.input :zipcode, :label =>"Código Postal" %>
                <%= f.input :zipextension, :label => "Ext." %>
          <div class="clear geo"></div>
                <%#= f.input :is_active_location %>
                <%=f.hidden_field :latitude%>
                <%=f.hidden_field :longitude%>
                <%= f.input :country, :as => :string, :label => "País", :input_html => {:default => "Portugal"} %>

    <% end -%>

  <%  f.buttons do %>
    <input id="map_center" type='button' onclick='getResults();' value='Centrar na morada' />
    <%= f.commit_button "Enviar"%>
    <h1><%= link_to "Cancelar", :back %></h1>
  <%end%>

我的控制台输出:

Processing GeographicLocationsController#create (for 127.0.0.1 at 2010-07-08 17:37:01) [POST]
  Parameters: {"commit"=>"Enviar", "action"=>"create", "authenticity_token"=>"z6gWF87u5hytrtXXsFAHKVl6fag3L3YmBKsfXcLqyKI=", "user_id"=>"72", "controller"=>"geographic_locations", "geographic_location"=>{"city"=>"", "latitude"=>"", "location"=>"", "country"=>"Portugal", "zipcode"=>"", "street"=>"", "longitude"=>"", "zipextension"=>""}}
  [4;36;1mUser Load (20.8ms)[0m   [0;1mSELECT * FROM "users" WHERE ("users"."persistence_token" = '4c46dcae34068fdb3bcf411a2f9498ad964137f3e9b6e4b9cfb9a64832b8bcefd9c406d8b0a678af93f9159dc59d4931a7ea404c67c744aad60cfb542c0ffbe1') LIMIT 1[0m
  [4;35;1mUser Load (0.4ms)[0m   [0mSELECT * FROM "users" WHERE ("users"."id" = 72) [0m
  [4;36;1mRole Load (0.4ms)[0m   [0;1mSELECT "roles".* FROM "roles" INNER JOIN "assignments" ON "roles".id = "assignments".role_id WHERE (("assignments".user_id = 72)) [0m
  [4;35;1mRole Load (0.2ms)[0m   [0mSELECT * FROM "roles" WHERE ("roles"."name" = 'admin') LIMIT 1[0m
Rendering template within layouts/application
Rendering geographic_locations/new

2 个答案:

答案 0 :(得分:1)

问题是每次渲染表单时都要传入一个新实例化的GeographicLocation。您应该使用您在控制器中创建的位置的实例,并通过编辑提交进行更新,如下所示:

<% semantic_form_for [@locatable, @geographic_location] do |f| %>

并在新动作中执行以下操作:

@locatable = find_locatable
@geographic_location = @locatable.geographic_locations.build(params[:geographic_location])

答案 1 :(得分:0)

您可能需要在表单声明中包含<%= f.error_messages %>

相关问题