Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation Rails 4如何使用carrierwave验证嵌套属性(多个图像/图片)的数量_Validation_Ruby On Rails 4_Limit_Carrierwave_Nested Attributes - Fatal编程技术网

Validation Rails 4如何使用carrierwave验证嵌套属性(多个图像/图片)的数量

Validation Rails 4如何使用carrierwave验证嵌套属性(多个图像/图片)的数量,validation,ruby-on-rails-4,limit,carrierwave,nested-attributes,Validation,Ruby On Rails 4,Limit,Carrierwave,Nested Attributes,我是stackoverflow&Rails的新手。我有三个模型,用户,帖子,帖子附件(创建帖子附件脚手架)我想做的是,每个帖子(帖子标题、内容、图片)用户最多只能上传5张图片(我指的是每篇帖子)。我参考了这一点来实现多张图片,基本上遵循了这一代码。我还发现这可以验证嵌套的_属性,但只有图片验证(嵌套的_属性)不起作用。任何想法都将不胜感激。谢谢 User.rb: class User < ActiveRecord::Base has_many :posts, dependent: :

我是stackoverflow&Rails的新手。我有三个模型,用户,帖子,帖子附件(创建帖子附件脚手架)我想做的是,每个帖子(帖子标题、内容、图片)用户最多只能上传5张图片(我指的是每篇帖子)。我参考了这一点来实现多张图片,基本上遵循了这一代码。我还发现这可以验证嵌套的_属性,但只有图片验证(嵌套的_属性)不起作用。任何想法都将不胜感激。谢谢

User.rb:

class User < ActiveRecord::Base
   has_many :posts, dependent: :destroy 
end
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
class PostAttachment < ActiveRecord::Base
   mount_uploader :picture,PictureUploader 
   belongs_to :post
end
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

您可以将条件放在
create
edit
方法之前,也可以放在
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
def创建
@post=当前用户.posts.build(post参数)
如果@post.save
##@post\u attachments=PostAttachment.find\u by\u post\u id(@post.id)->这可以在编辑中使用
##如果参数[:post_attachments]['picture'].length+@post_attachment.length@post.id)
结束
elsif参数[:post_附件][“图片”]。长度>5
flash[:error]=“您的邮件”
重定向到您的路径
返回错误
结束
将_重定向到@post,注意:“post Saved”
其他的
如果参数[:post_附件][“图片”]。长度>5

@post.errors[:base]您好,谢谢您的帮助。我改变了你教给我的创建方法。这似乎有效,但我仍然有问题。在我用5张图片创建帖子后,它会显示视图(posts/show.html.erb)和5张带有消息“post Saved”的图片(这是正确的态度)。但当我发布超过5张图片时,它会显示消息“post saved”,并显示没有图片的视图(因为我更改了创建方法)。如果图片超过5张,我不想保存帖子(标题、内容、图片)。我想在我的post.rb中显示消息(“最多5条消息”),而不是显示视图(post应该无法保存文件),我现在有2个验证。验证:title,presence:true,validates:content,presence:true如果您不想保存超过5张图片的帖子,可以将
elsif参数[:post_attachments]['picture']放入。长度>=5
flash[:error]=“您的邮件…”
重定向到新的
返回false
您好谢谢您的建议!!你的答案在某种程度上有效,但我仍然有问题。正如我所说,我在Post.rb中有2个验证(title、presence&content、presence)。例如,在我的表单页面(app/views/posts/_form.html.erb)中,我有标题、内容、图片上传器,如果我发布了5张以上的图片和空标题,它会显示“title不能为空”消息(因为验证首先出现),并且不会显示“最多5条消息”消息。如果我用超过5张图片写出正确的标题和内容(不是空白),您的代码将显示错误消息。我想说的是,错误消息应以相同的格式显示。我将更新我的答案。检查这是否有效<代码>@post.errors[:base]
<% if @post_attachments.length <= 5 %>
   <%= f.file_field %>
<% end %>