2010-07-08 5 views
0

다형성 연관에 대한 모든 정보가 있지만 모델 유효성 검사를 수행하면 오류가 표시되지 않습니다. 내 컨트롤러에서레일 다형 모델. 보기에서 양식 유효성 검사 오류를 표시하는 방법?

나는이 :

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 => 'edit'가 발생하면 오른쪽 페이지로 이동하지만 오류는 나타나지 않습니다.

내 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 
+0

확인 기본적인 질문 : 당신의 새로운 액션에 다음

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

및 수행 다음과 같이 제출에 당신은 편집을 통해 당신이 컨트롤러에서 만들어진 위치와 업데이트의 인스턴스를 사용한다 보기 코드를 표시하지 않았으므로 뷰에'f.error_messages' 출력과 같은 폼 빌더 에러를 포함 시켰습니까? – bjg

+0

formtastic을 사용하고 있으므로 필요하지 않습니다. 나는 formtastic 방법을 사용하여 다른 모든 다형 양식에 에로스를 얻는다. 그래도 디버그를 위해 <% = f.error_messages %>를 삽입했지만 오류는 나타나지 않습니다. –

+0

설정 한 모델과 다형성 관계를 볼 수 있습니까? 위에 표시된 유효성 확인은 어떤 모델에 속합니까? – bjg

답변

1

양식을 렌더링 할 때마다 새로 인스턴스가 생성 된 GeographicLocation을 전달하는 것이 문제입니다.

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

양식을 렌더링 할 때마다 새 모델을 작성하기 때문에 모델을 저장하려고 할 때 #create 할 모델에 오류가 없습니다. – Winfield

+0

그게 다야! 정말 고맙습니다! –

0

양식 선언 내부 <%= f.error_messages %>을 포함해야 할 수도 있습니다.

+0

나는 사용자 formtastic 그래서 그게 아니야. 다른 모든 양식 (비 다형성)은 정상적으로 작동하고 오류를 표시합니다. –

관련 문제