I have everything on my polymorphic associations working, but if I have model validations, the erros don't show up.
In my controller I have:
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
And in the Model:
class GeographicLocation < ActiveRecord::Base
belongs_to :locatable, :polymorphic => true
validates_presence_of :city, :message =&开发者_开发技巧gt; "Falta Cidade"
validates_presence_of :location
class User < ActiveRecord::Base
has_many :geographic_locations, :as => :locatable
Very standard stuff. But the fact is, when the render :action => 'edit' happens, it goes to the right page, but the errors don't show up.
My new.html.erb view:
<% 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%>
My console output:
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
The problem is that you're passing in a newly instantiated GeographicLocation every time you render the form. You should use the instance of the location you made in the controller and update via edit on submit, like the following:
<% semantic_form_for [@locatable, @geographic_location] do |f| %>
and do the following in your new action:
@locatable = find_locatable
@geographic_location = @locatable.geographic_locations.build(params[:geographic_location])
You might need to include <%= f.error_messages %>
inside the form declaration.
精彩评论