2011-07-27 211 views
2

我已經嵌套accepts_nested_attributes_for兩種型號:錯誤消息不顯示回形針驗證錯誤消息

class Place < ActiveRecord::Base 
    # Relations.. 
    accepts_nested_attributes_for :place_photos, :allow_destroy => true 
    # others methods... 
end 

-

class PlacePhoto < ActiveRecord::Base 

    belongs_to :place 
    # attr_accessible :photo_index                                
    has_attached_file :image, :styles => { :small => "120>", :large => "640x480>"} 
    validates_attachment_presence :image 
    validates_attachment_size :image, :less_than => 500.kilobytes, 
     :if => Proc.new {|imports| !imports.image_file_name.blank? }, 
     :message => "This message probably won't be shown! :P" 
end 

這裏是添加圖像的觀點:

<%= form_for([:profile, @place], :html => {:multipart => true }) do |f| %> 

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

      <ul> 
      <% @place.errors.full_messages.each do |msg| %> 
       <li><%= msg %></li> 
      <% end %> 
      </ul> 

     </div> 
     <% end %> 
# ... 
      <%= f.fields_for :place_photos do |p| %> 
      <% if p.object.new_record? %> 
       <div> <%= image_tag("place_avatar.png", :class => "listTabsImg", :height => '120', :width => '120') %> 
       <div class="listTabsTxt"> 
        <span>Upload new image</span> <br/> 
        <%= p.file_field :image %><br/> 
        <div class="clear"></div> 
       </div> 
       </div> 
       <br/> 
      <% end %> 
      <% end %> 

<%= f.submit "Upload all", :class => "btnGB" %> 
<% end %> 

不幸的回形針驗證錯誤信息沒有顯示(即「這個信息可能不會顯示!:P 「)。

這是可以解決這個問題嗎?

回答

3

該問題出現在控制器中。我使用:的

redirect_to photos_path

代替:

render :action => "edit"

redirect_to的覆蓋@object(直接調用編輯動作),因此@object.errors清潔。

當我看到那些,我只是大聲地說。 :D