使用carrierwave在轨道上上载图像

时间:2017-06-15 19:33:03

标签: ruby-on-rails carrierwave minimagick

所以我正在开发一个简单的分类应用程序在轨道上。我正在尝试通过 carrierwave minimagick 实现图片上传,这是一个很好的文档,我已经在线跟踪了很多教程,他们或多或少都提供相同的解决方案使用 rails SQlite 开发的应用。

实现我的代码后,我没有收到任何错误消息或警告,它只是不上传图像。当我使用 DB Browser for SQlite 查找我的数据库时,除了显示为null的图像外,其他所有字段都会被填写。

我不将 images 属性作为单独的模型包含在内。我希望它们只是我的广告模型

的一部分

PS。这是我在Stackoverflow上的第一篇文章,所以请耐心等待。除了图片上传外,其他所有工作都有效。

new.html.erb

<div class="new_ad_form row">
<div class="col-lg-6 col-lg-offset-3">

 <%= form_for(@ad, :html => { :multipart => true }) do |f|%> 
 <%= render 'layouts/shared/error_messages', object: f.object %>
    <div class= "new_ad_field">
    <%= f.label "title:" %>  <!-- f.object here refers to @ad -->
    <%= f.text_field :title %><br>
    </div>
    <div class= "new_ad_field">
    <%= f.label :category %>
    <%= f.select :category, @categories %><br>
    </div>
    <div class= "new_ad_field">
    <%= f.label :price %>
    <%= f.number_field(:price, in: 1.0..1000000, step: 0.5) %>
    </div>
    <div class= "new_ad_field">
    <%= f.label :description %>
    <%= f.text_area :description, size: "30x5" %>
    </div>
    <div class= "new_ad_field">
    <%= f.label :address %>
    <%= f.text_field :address %><br>
    </div>
    <div class= "new_ad_field">
    <%= f.label :state %>
    <%= f.select :state, @states, {}, :onChange=>"javascript: 
    $dc.select_city();" %>
    </div>
    <div class= "new_ad_field">
    <%= f.label :city %>
    <%= f.select :city, @cities %><br>
    </div>
    <div class="field">
        <%= f.label :images %><br>
        <%= f.file_field :images, :multiple => true %>
    </div>
    <div style="text-align: center"><%= f.submit "Post ad", class: "btn btn-
       large btn-primary" %></div>
 <% end %>
</div>
</div>

ads_controller.rb

class AdsController < ApplicationController
 def new
  #@user = current_user
  @ad = Ad.new
  @categories = ['Electronic equipment', 'Cars', "Clothing", "Phones"]
  @location = {"Abia": ["Abuja","Gusau", "Kaduna", "Kano", "Yola", "Lagos", 
             "Minna", "Keffi"],
             "Zamfara": ["Abuja","Gusau", "Kaduna", "Kano", "Yola", "Lagos", 
             "Minna", "Keffi"]}

  @states = @location.keys
  @cities = @location[@location.keys[0]]

 end

def show
  @user = current_user
  @ads = @user.ads.paginate(page: params[:page])
end

def create

  @ad = current_user.ads.build(ad_params)
  if @ad.save
    flash[:success] = "ad created!"
    redirect_to current_user
  else
    render 'static_pages/home'
  end

private

  def ad_params
    params.require(:ad).permit(:user_id, :title, :category, :description, 
    :price, :address, :city, :state, {images: []})
  end

end

ad.rb

class Ad < ApplicationRecord
belongs_to :user

validates :user_id, presence: true
#validates :images, presence: true

mount_uploader :images, ImagesUploader
serialize :images, JSON
end

images_uploader.rb

class ImagesUploader < CarrierWave::Uploader::Base

 include CarrierWave::MiniMagick

 storage :file

    def store_dir
      "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end

   version :thumb do
     process :resize_to_fit => [50, 50]
   end

   def extension_white_list
     %w(jpg jpeg gif png)
   end

end

当然在我的gemfile中我有

....................
  gem 'carrierwave' 
  gem 'mini_magick'
....................

配置/初始化/ carrier_wave.rb

require 'carrierwave'
require 'carrierwave/orm/activerecord'

以下是命令行日志。基本上我正在试图找出它为什么要回滚交易。

Started POST "/ads" for 127.0.0.1 at 2017-06-19 14:22:00 -0400
Processing by AdsController#create as HTML
  Parameters: {"utf8"=>"✓", 
"authenticity_token"=>"7ehxs5JEkuhVasFB0m8VQkVbPLEc1+q/xPJQMD1H5Fblw==", 
"ad"=>{"title"=>"sdasdfasdfaasdf", 
"category"=>"Electronic equipment", "price"=>"500", 
"description"=>"asdfasdfasdfwetqwerfasdfasdcvzx", 
"address"=>"asdfafawefasdfadf", "state"=>"Kaduna", "city"=>"Zaria", 
"images"=>[#
<ActionDispatch::Http::UploadedFile:0x0000001041d1a0 @tempfile=#
<Tempfile:C:/Users/SAMSUNG/AppData/Local/Temp/RackMultipart20170619-8456-
1i97f2t.jpg>, 
@original_filename="394502_10152051195495464_1783658057_n.jpg", 
@content_type="image/jpeg", @headers="Content-Disposition: form-data; 
name=\"ad[images][]\"; 
filename=\"394502_10152051195495464_1783658057_n.jpg\"\r\nContent-Type: 
image/jpeg\r\n">]}, "commit"=>"Post ad"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? 
ORDER BY "users"."id" ASC LIMIT ?  [["id", 24], ["LIMIT", 1]]
   (0.0ms)  begin transaction
   (0.0ms)  rollback transaction
No template found for AdsController#create, rendering head :no_content
Completed 204 No Content in 1706ms (ActiveRecord: 1.0ms)

2 个答案:

答案 0 :(得分:0)

我相信您需要在代码中更改以下行:

mount_uploader :images, ImagesUploader

mount_uploader应该这样复数:

mount_uploaders :images, ImagesUploader

答案 1 :(得分:-1)

您需要在config/application.rb文件

中添加一些代码
require 'carrierwave'
require 'carrierwave/orm/activerecord'

我认为它的工作尝试了这个