当前位置: 动力学知识库 > 问答 > 编程问答 >

file upload - rails 3.1 paperclip

问题描述:

I am getting started with the paperclip plugin, but I can't seem to get an image to show up in development. I added the following to my config/environments/development.rb file:

Paperclip.options[:command_path] = "/usr/local/bin/"

I installed imagemagick and ghostscript through the homebrew package manager. I then added the paperclip gem to my gemfile and ran bundle install. I then ran

rails g paperclip carrier_data asset

rake db:migrate

I then went to my carrier data model and added

has_attached_file :asset

After that I altered the form partial to add the resulting code so I could upload images, I am using the simple form gem and this is the following code that I have entered.

<%= simple_form_for [:carrier_datum, @carrier_datum], :url => carrier_datum_path, :html => { :multipart => true} do |f| %>

<div>

<%= f.input :asset, :as => :file, :label => "Upload File" %>

</div>

Finally I went to the show page and added the following code so I could view the image:

<%= image_tag @carrier_datum.asset.url %>

When I try to create a new carrier datum record I get this message:

No route matches {:action=>"show", :controller=>"carrier_data"}

The logs look like the following:

ActionView::Template::Error (No route matches {:action=>"show", :controller=>"carrier_data"}):

1: <%= simple_form_for [:carrier_datum, @carrier_datum], :url => carrier_datum_path, :html => { :multipart => true} do |f| %>

2: <% if @carrier_datum.errors.any? %>

3: <div id="error_explanation">

4: <h2><%= pluralize(@carrier_datum.errors.count, "error") %> prohibited this carrier_datum from being saved:</h2>

app/views/carrier_data/_form.html.erb:1:in `_app_views_carrier_data__form_html_erb__2372678434167691626_2513245300'

app/views/carrier_data/new.html.erb:3:in `_app_views_carrier_data_new_html_erb___55769200685419080_2513267980'

app/controllers/carrier_data_controller.rb:30:in `new'

I am running rails 3.1, with postgresql as my database if that helps at all.

Here is the output of rake routes

carrier_data GET /carrier_data(.:format) {:action=>"index", :controller=>"carrier_data"}

POST /carrier_data(.:format) {:action=>"create", :controller=>"carrier_data"}

new_carrier_datum GET /carrier_data/new(.:format) {:action=>"new", :controller=>"carrier_data"}

edit_carrier_datum GET /carrier_data/:id/edit(.:format) {:action=>"edit", :controller=>"carrier_data"}

carrier_datum GET /carrier_data/:id(.:format) {:action=>"show", :controller=>"carrier_data"}

PUT /carrier_data/:id(.:format) {:action=>"update", :controller=>"carrier_data"}

DELETE /carrier_data/:id(.:format) {:action=>"destroy", :controller=>"carrier_data"}

pages GET /pages(.:format)

If I remove the simple form call and use regular form partial notation like this:

<%= form_for(@carrier_datum, :html => { :multipart => true}) do |f| %>

<div>

<%= f.label :asset, "Upload File:"%>

<%= f.file_field :asset %>

</div>

I still get the broken image issue, and my logs say that the image is missing.

网友答案:

I ended up switching to carrierwave, and I must say it it a much better gem for file uploads. If you are working on rails 3.1 it is a better file upload solution.

网友答案:

In the model you need to specify where to put uploaded files

For example:

has_attached_file :asset,
:url  => "/assets/:id/:basename.:extension",
:path => ":rails_root/public/assets/:id/:basename.:extension"

In the public folder of rails, create a folder "assets".

Each upload will create folders inside with an id and the file uploaded.

分享给朋友:
您可能感兴趣的文章:
随机阅读: