Rails 4 如何使用载波验证嵌套属性的数量(多个 image/picture)
Rails 4 How to validate the number of nested attributes(multiple image/picture) using carrierwave
我是 Whosebug 的新手 & Rails。我有三个模型,User,Post,post_attachment(Create post_attachment scaffold)。 我想做的是,每个post(post的标题,内容,图片)用户最多只能上传5张图片(我的意思是每个post)。 我引用这个 article to achieve multiple image and basically followed this code. I also found this article 来验证 nested_attributes 但只有图片验证 (nested_attributes) 不起作用。任何想法,将不胜感激。谢谢。
User.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
您可以将条件放在 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
您还可以在编辑 post 时查看视图。
<% if @post_attachments.length <= 5 %>
<%= f.file_field %>
<% end %>
我是 Whosebug 的新手 & Rails。我有三个模型,User,Post,post_attachment(Create post_attachment scaffold)。 我想做的是,每个post(post的标题,内容,图片)用户最多只能上传5张图片(我的意思是每个post)。 我引用这个 article to achieve multiple image and basically followed this code. I also found this article 来验证 nested_attributes 但只有图片验证 (nested_attributes) 不起作用。任何想法,将不胜感激。谢谢。
User.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
您可以将条件放在 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
您还可以在编辑 post 时查看视图。
<% if @post_attachments.length <= 5 %>
<%= f.file_field %>
<% end %>