【问题标题】:Error messages don't show Paperclip validations error messages错误消息不显示回形针验证错误消息
【发布时间】:2011-07-27 10:52:04
【问题描述】:

我有两个模型嵌套在 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”)。

这可以解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 paperclip nested-attributes


    【解决方案1】:

    问题出在控制器中。我用:

    redirect_to photos_path

    代替:

    render :action =&gt; "edit"

    redirect_to 覆盖@object(直接调用编辑操作),所以@object.errors 被清除。

    当我看到那个时,我只是笑了。 :D

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多