Setting up Rails 3 + Polymorphic image model + Clip and Amazon S3, No errors, but nothing loads

I suspect the problem is how I create the polymorphic image attribute. I am using fields_for in the form. In this case, the user can create a message and add the image using paperclip, saving it from S3. I am using the polymorphic post_image image model:

 class PostImage < ActiveRecord::Base belongs_to :imageable, :polymorphic => true #.merge(PAPERCLIP_OPS) has_attached_file :image, :styles => { :medium => "200x200>", :thumb => "50x50>" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :path => "/:style/:id/:filename", :bucket => "zounds-dev" validates_attachment_presence :image validates_attachment_size :image, :less_than => 5.megabytes end 

Message Model:

 class Post < ActiveRecord::Base has_many :post_images, :as => :imageable, :dependent => :destroy . . . accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true end 

New message form:

 =form_for( setup_post(@post,current_user), :html => { :multipart => true}) do |f| %dl =f.fields_for :post_images do |ff| =ff.file_field :image %dt.field=f.label :name %dd.field=f.text_field :name %dt.field=f.label :description %dd.field=f.text_area :description =f.fields_for :user do |u| =render "user_fields", :f => u =f.fields_for :assignments do |ff| =ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0','1' =ff.label :_destroy, ff.object.group.name =ff.hidden_field :group_id .action=f.submit "Save Post" 

The setup_post helper method used by Post form_for : (group elements are not relevant here)

  def setup_post(post, current_user) groups = current_user.groups_as_owner + current_user.groups_as_member (groups - post.groups).each do |group| post.assignments.build(:group => group) end post.assignments.sort_by {|x| x.group.name } post_image = post.post_images.build post end 

Mail Controller:

  def new @user = User.find(params[:user_id]) # @post = @user.posts.build @post = Post.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @post } end end def create @user = current_user @post = @user.posts.build(params[:post]) . . . end 

I suspect the problem is that I am using fields_for for the post_image attribute, but I looked through everything and cannot figure out how to correctly implement the polymorphic nested image attribute.

I also made a console console s3sh amazon s3, and although I could not load the image because I could not figure out how to go the correct way of the image to the open () function, I connected to S3, My s3.yml file s3.yml also configured correctly.

Thanks yall, Brian

+6
source share
1 answer

The problem was reject_if in accepts_nested_attributes for the Post model

accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true

commenting on this, fixed the problem.

+1
source

Source: https://habr.com/ru/post/907812/


All Articles