Rails 4:如何在未选择图像时显示file_field和error_messages

时间:2014-08-17 04:24:30

标签: ruby-on-rails ruby-on-rails-4 carrierwave

<%= p.file_field :image %>,当我点击_article_form.html.erb中的提交按钮而没有输入任何内容时,系统不会显示错误消息。

<%= f.text_area :content, placeholder: "enter..." %>并显示此text_area的错误消息。

如何显示<%= p.file_field :image %>和错误消息?

视图\物品\ new.html.erb

<div class="row">
  <div class="span8">
    <%= render 'shared/article_form' %>
  </div>
</div>

视图\ shared_article_form.html.erb

<%= form_for(@article) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.fields_for :photos do |p| %>
      <% if p.object.image and p.object.image.file %>
        <%= image_tag p.object.image.thumb.url %>
        <p><%= p.object.image.file.filename %></p>
      <% end %>
      <%= p.file_field :image %>
      <br>
    <% end %>
    <%= f.text_area :content, placeholder: "enter..." %>
  </div>
  <%= f.submit class: "btn btn-large btn-primary" %>
<% end %>

\控制器\ articles_controller.rb

class ArticlesController < ApplicationController

  def new
    @article = Article.new
    .
    .
    .
    3.times { @article.photos.build }
  end

  def create
    @article = current_user.articles.build(article_params)
    if @article.save
      flash[:success] = "article created!"
      redirect_to current_user
    else
       render 'new'
    end
  end
    .
    .
    .
  private

    def article_params
      params.require(:article).permit(:content, :stock_id, photos_attributes: [:id, :article_id, :image])
    end

end

\模型\ article.rb

class Article < ActiveRecord::Base
    .
    .
    .
    accepts_nested_attributes_for :photos, reject_if: proc { |attributes| attributes[:image].blank? }
    validates :content, presence: true, length: { maximum: 140 }
    .
    .
    .
end

\模型\ photo.rb

class Photo < ActiveRecord::Base
    belongs_to :article
    mount_uploader :image, ImageUploader
    validates :image, presence: true
end

更新

我编辑了代码,目前情况如下。

当我单击没有内容且没有图像的提交按钮时,会显示错误消息和f.text_area :contentp.file_field :image未显示。

当我单击包含内容但没有图像的提交按钮时,会显示错误消息和f.text_area :contentp.file_field :image未显示。

当我单击没有内容和多个图像的提交按钮时,会显示错误消息和f.text_area :content。还会显示p.file_field :image,具体取决于图像的数量。尽管我选择的图像数量很多,但我总是会显示3 p.file_field :image

当我点击包含内容和多个图片的提交按钮时,它可以正常工作。

\模型\ article.rb

class Article < ActiveRecord::Base
.
.
    accepts_nested_attributes_for :photos, reject_if: proc { |attributes| attributes[:image].blank? }
    validates :content, presence: true, length: { maximum: 140 }
.
.
    validate :check_for_at_least_image

    def check_for_at_least_image
      errors.add(:image, " select at least one") if self.photos.size <= 0
    end

\模型\ photo.rb

class Photo < ActiveRecord::Base
    belongs_to :article
    mount_uploader :image, ImageUploader
#   validates :image, presence: true    #comment out this line
end

2 个答案:

答案 0 :(得分:1)

由于您使用的是接受嵌套属性,因此只需要在photo.rb中的图像字段上添加验证,如果用户没有在表单中输入图像,那么它会向您显示错误,因此您的photos.rb将如下所示:

class Photo < ActiveRecord::Base
  belongs_to :article
  validates :image, presence: true
end

<强>更新

您的自定义验证将是这样的:

class Article < ActiveRecord::Base
  .
  .
  validate :check_for_at_least_image

  def check_for_at_least_image
      errors.add(:image, "select at least one") if self.photos.size <= 0
  end
end

<强>更新

由于某种原因,文章在验证错误后没有构建您的嵌套属性,所以在您的创建操作中,您可以这样做:

def create
  @article = current_user.articles.build(article_params)
  if @article.save
    flash[:success] = "article created!"
    redirect_to current_user
  else
     3.times { @article.photos.build } if @article.photos.blank? #this will build your images if it's empty and if it isn't then it'll simply render your file_fields
     render 'new'
  end
end

答案 1 :(得分:0)

查看reject_if的{​​{1}}选项。 您拒绝在没有图像存在的情况下构建照片。因此,照片永远不会被构建,没有什么可以验证的,也没有任何错误来自。