我成立了一个story
模型由回形针处理的image
附件后的“新”行动,看起来像文件路径:回形针(3.0)和Rails(3.2):f.file_field失去一个验证错误
class Story < ActiveRecord::Base
has_attached_file :image # [...]
attr_accessible :user_id, :title, :image, :image_file_name
belongs_to: user
validates_presence_of :user_id
validates :title, :length => { :maximum => 50 }
validates_attachment_size :image, :less_than => 2.megabytes, :unless => Proc.new { |story| story[:image].nil? }
# [...]
end
当我填写我的故事的形式,看起来像:
<%= form_for @story, html: { multipart: true } do |f| %>
<% if @story.errors.any? %>
<div id="error-explanation">
<ul>
<% @story.errors.full_messages.each do |msg| %>
<li class="error-mess">Error: <%= msg.downcase %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :title %></td>
<%= f.file_field :image %>
<%= f.submit t('.send') %>
<% end %>
如果验证了story.title过长形式沿着正确与正确的错误消息和无效TITL重新显示失败e已经填写,,但file_field
现在是空白的,我必须再次点击它才能重新选择我想上传的文件。
这里是我的stories_controller.rb的样子:
def create
@story = @current_user.stories.new(params[:story])
if @story.save
redirect_to thanks_path
else
# [email protected] so I render action 'new' again just to
# bang my head against this 'anomaly'
render action: "new"
end
end
我怎样才能避免重新选择文件验证错误后上传用户?
我面临同样的问题,你有没有找到一个解决办法?谢谢! – yorch
只有一个图形:选择图片时,我通过jQuery提供UI反馈。假设我的图像字段是由'<%= f.file_field:image%>生成的,我可以使用下面的jQuery来触发它:'$(“#story_image”)。change(function(){$(“#image_selected “).show(300);});'然后在我看来,我有一个隐藏的div”image_selected“,其中包含一个雄辩的gif ...如果您发现_real_解决方案,请告诉我! – Darme