0

i'm new to stackoverflow & Rails. I have three models, User, Post, post_attachment(Create post_attachment scaffold). What i want to do is , every post(Title of post,Content,Picture) user can only upload 5 pictures maximum(i mean each post). I referred this article to achieve multiple image and basically followed this code. I also found this article to validate nested_attributes but only picture validation(nested_attributes) is not working. Any ideas would be appreciated. Thanks.

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
Community
  • 1
  • 1
arthur
  • 75
  • 8

1 Answers1

0

You can put condition before create and edit methods and also in edit.html.erb. like if you want to save maximum 5 photos you can use

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

also you can check in view while editing your post.

<% if @post_attachments.length <= 5 %>
   <%= f.file_field %>
<% end %>
Tanay Sharma
  • 1,098
  • 10
  • 29
  • Hi ts , thanks for help. I changed create method like you showed me. It seems working but i still have problem. After i create post with under 5 pictures , it shows view (posts/show.html.erb) and 5 pictures with message "Post Saved"(This is correct attitude). But when i post over 5 pictures it shows message " post saved" and showing view with no pictures (because i changed Create method). I don't wanted post(title,content,picture) to be saved if picture is over 5 pictures. Instead of showing view(post should fail save file ) I wanted to show message ("5 message maximum") – arthur Apr 18 '17 at 09:09
  • In my Post.rb i have 2validates now. validates :title, presence: true, validates :content, presence: true – arthur Apr 18 '17 at 09:11
  • If you do not want to save post over 5 pictures , you can put `elsif params[:post_attachments]['picture'].length >= 5` `flash[:error] = 'Your message..!'` `redirect to new` `return false` – Tanay Sharma Apr 18 '17 at 09:33
  • Hi ts thanks for advice !! Your answer is working in some part but i still have problem. Like i said i have 2validates in Post.rb.(title, presence & content, presence).For example ,In my form page(app/views/posts/_form.html.erb) i have Title, Content ,Picture uploader ,if i post over 5 pictures and empty title it shows "Title can't be blank" message(because validate comes first) and doesn't show "5 message maximum" message. Your code shows error message if i write correct Title and Content (not blank) with over 5pictures .What i want say is that error message should show in same format . – arthur Apr 25 '17 at 09:27
  • I update my answer .check if that is working or not. `@post.errors[:base] << "Maximum 5 messages/pictures." ` You just simply add custom error message with your Post model errors – Tanay Sharma Apr 25 '17 at 10:21
  • Hi ts. Thanks again. Your update answer gives correct error message now, if i post over 5 pictures. But if i don't post any picture(blank) and submit, it gives NoMethodError in PostsController#Create with "undefined method '[]' for nil:NilClass". – arthur Apr 26 '17 at 09:27
  • Hi Arthur, Can you share the screenshot of error , at which line do you get error. or you can contact me at ttss2627@gmail.com if you want. – Tanay Sharma Apr 26 '17 at 10:47