【问题标题】:Rails 4 How to validate the number of nested attributes(multiple image/picture) using carrierwaveRails 4如何使用carrierwave验证嵌套属性的数量(多个图像/图片)
【发布时间】:2017-04-16 08:40:24
【问题描述】:

我是 stackoverflow 和 Rails 的新手。我有三个模型,User、Post、post_attachment(创建 post_attachment 脚手架)。 我想做的是,每个帖子(帖子标题,内容,图片)用户最多只能上传5张图片(我的意思是每个帖子)。我参考了这个article来实现多张图片和基本上遵循这个代码。我还发现这个article 来验证nested_attributes 但只有图片验证(nested_attributes)不起作用。任何想法,将不胜感激。谢谢。

用户.rb:

class User < ActiveRecord::Base
   has_many :posts, dependent: :destroy 
end

Post.rb:

class Post < ActiveRecord::Base
   belongs_to :user
   has_many :post_attachments
   accepts_nested_attributes_for :post_attachments,allow_destroy: true, reject_if: :all_blank
end

Post_attachment.rb:

class PostAttachment < ActiveRecord::Base
   mount_uploader :picture,PictureUploader 
   belongs_to :post
end

Post_controller.rb:

def new
   @post = Post.new
   @post_attachment = @post.post_attachments.build
end

def show
   @post_attachments = @post.post_attachments.all
end

def create
   @post = current_user.posts.build(post_params)

  if @post.save
    params[:post_attachments]['picture'].each do |a|
          @post_attachment = @post.post_attachments.create!(:picture => a, :post_id => @post.id)
    end
    redirect_to @post, notice: "Post Saved" 
   else
     render 'new'
  end
end

private

def post_params
   params.require(:post).permit(:title,:content,:user_id,post_attachments_attributes: [:id, :post_id, :picture, :_destroy])
end

【问题讨论】:

    标签: validation ruby-on-rails-4 limit carrierwave nested-attributes


    【解决方案1】:

    您可以将条件放在createedit 方法之前,也可以放在edit.html.erb 中。 比如如果你想保存最多 5 张照片,你可以使用

    def create
      @post = current_user.posts.build(post_params)
    
      if @post.save
        ##@post_attachments = PostAttachment.find_by_post_id(@post.id) --> this can be use in edit
        ##if params[:post_attachments]['picture'].length+ @post_attachment.length <= 5
         if params[:post_attachments]['picture'].length <= 5
           params[:post_attachments]['picture'].each do |a|
              @post_attachment = @post.post_attachments.create!(:picture => a, :post_id => @post.id)
           end
         elsif params[:post_attachments]['picture'].length > 5
           flash[:error] = 'Your message'
           redirect_to YOUR_PATH
           return false
         end
         redirect_to @post, notice: "Post Saved" 
      else
       if params[:post_attachments]['picture'].length > 5
          @post.errors[:base] << "Maximuim 5 messages/pictures."
       end
       render 'new'
     end
    end
    

    您也可以在编辑帖子时签入视图。

    <% if @post_attachments.length <= 5 %>
       <%= f.file_field %>
    <% end %>
    

    【讨论】:

    • 您好,感谢您的帮助。我改变了你给我看的创建方法。它似乎工作,但我仍然有问题。在我创建少于 5 张图片的帖子后,它会显示视图 (posts/show.html.erb) 和 5 张带有“帖子已保存”消息的图片(这是正确的态度)。但是当我发布超过 5 张图片时,它会显示消息“已保存”并显示没有图片的视图(因为我更改了创建方法)。如果图片超过 5 张图片,我不希望保存帖子(标题、内容、图片)。而不是显示视图(帖子应该保存文件失败)我想显示消息(“最多 5 条消息”)
    • 在我的 Post.rb 中,我现在有 2 个验证。验证:标题,存在:真,验证:内容,存在:真
    • 如果您不想保存超过5张图片的帖子,您可以输入elsif params[:post_attachments]['picture'].length &gt;= 5flash[:error] = 'Your message..!'redirect to newreturn false
    • 您好,感谢您的建议!您的答案在某些方面有效,但我仍然有问题。就像我说的那样,我在 Post.rb 中有 2 个验证。(标题、存在和内容、存在)。例如,在我的表单页面(app/views/posts/_form.html.erb)中,我有标题、内容、图片上传器,如果我发布超过 5 张图片且标题为空,则会显示“标题不能为空白”消息(因为首先验证)并且不显示“最多 5 条消息”消息。如果我用超过 5 张图片编写正确的标题和内容(非空白),您的代码会显示错误消息。我想说的是错误消息应该以相同的格式显示。
    • 我更新了我的答案。检查是否有效。 @post.errors[:base] &lt;&lt; "Maximum 5 messages/pictures." 您只需在 Post 模型错误中添加自定义错误消息
    猜你喜欢
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多