简体   繁体   中英

Rails 4 Paperclip Images not uploading/showing

I used paperclip gem to implement multiple image upload in my app. My models are as follows

class ProductImage < ActiveRecord::Base
    belongs_to :product
        has_attached_file :image ,:styles => {
  :thumb    => ['100x100#',  :jpg, :quality => 70],
  :preview  => ['480x480#',  :jpg, :quality => 70],
  :large    => ['600>',      :jpg, :quality => 70],
  :retina   => ['1200>',     :jpg, :quality => 30]
},:path => ':rails_root/public/system/:id.:extension',
:convert_options => {
  :thumb    => '-set colorspace sRGB -strip',
  :preview  => '-set colorspace sRGB -strip',
  :large    => '-set colorspace sRGB -strip',
  :retina   => '-set colorspace sRGB -strip -sharpen 0x0.5'
},
:url  => "/assets/books/:id/:style/:basename.:extension",
                  :path => ":rails_root/public/assets/books/:id/:style/:basename.:extension"
  validates_attachment_presence :image
    validates_attachment_size :image , :less_than => 10.megabytes
    validates_attachment_content_type :image , :content_type => ['image/jpeg','image/jpg','image/png']
do_not_validate_attachment_file_type :image
end
class Product < ActiveRecord::Base
    belongs_to :user
  belongs_to :category
    has_many :comments , dependent: :destroy
    has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }
end

product_controller.rb

class ProductsController < ApplicationController
  def new
    @product = current_user.products.new
    3.times {@product.product_images.build}
  end

  def create
    @product = current_user.products.new
    3.times { @product.product_images.build }
    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:name, :price, :description, :reason, :user_id,:status,:category_id,product_images_attributes: [:image])
    end

    def correct_user
      @product = current_user.products.find_by(id:  params[:id])
      redirect_to root_url if @product.nil?
    end
end

_form.html.erb

<%= form_for @product, url: products_path, :html => {:multipart => true,:class => " form-horizontal center"} do |f| %>

<%= f.fields_for :product_images do |builder| %>
  <p>
    <%= builder.label :image, "Image File" %>
    <%= builder.file_field :image %>
  </p>
<% end %>
  <div class="actions">
    <%= f.submit "Submit", class: "btn btn-default btn-primary" %>
  </div><br/>
<% end %>

show.html.erb

<% @products.each do |product| %>
      <% product.product_images.each do |picture| %>
      <%= picture.image.url %>
   <% end %> 

When i submit files page is not redirected to homepage.i cant even see errors.Can somebody explain me whats the problem in the code

The one thing i notice is in the product controller create action:

@product = current_user.products.new 

should be:

@product = current_user.products.build(product_params)

you should always pass in product_params when creating new object in the create action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM