开发者

Getting Paperclip to work in Rails

开发者_如何转开发

I have installed the Paperclip plugin to attempt to upload an avatar for my kase model. For some reason, the select the file button shows, and I can choose a file - but then when I click update the kase - it takes me to the show page, but the missing.png rather than the selected image.

kase.rb

class Kase < ActiveRecord::Base
def self.all_latest
find(:all, :order => 'created_at DESC', :limit => 5)
end
def self.search(search, page)
paginate :per_page => 5, :page => page,
:conditions => ['name like ?', "%#{search}%"],
:order => 'name'
end
 # Paperclip
 has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end

kases_controller.rb

# GET /kases/new
# GET /kases/new.xml
def new
 @kase = Kase.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @kase }
end
end

new.html.erb

<% content_for :header do -%>

Cases <% end -%>

<% form_for(@kase) do |f| %>
<%= f.error_messages %>

<ul id="kases_new">

<li> <%= f.file_field :avatar %></li>

<li>Job Ref.<span><%= f.text_field :jobno %></span></li>

<li>Case Subject<span><%= f.text_field :casesubject %></span></li>

<li>Transport<span><%= f.text_field :transport %></span></li>

<li>Goods<span><%= f.text_field :goods %></span></li>

<li>Date Instructed<span><%= f.date_select :dateinstructed %></span></li>

<li>Case Status<span><%= f.select "kase_status", ['Active', 'On Hold', 'Archived'] %>       </span></li>

<li>Client Company Name<span><%= f.text_field :clientcompanyname %></span></li>

<li>Client Company Address<span><%= f.text_field :clientcompanyaddress %></span></li>

<li>Client Company Fax<span><%= f.text_field :clientcompanyfax %></span></li>

<li>Case Handler Name<span><%= f.text_field :casehandlername %></span></li>

<li>Case Handler Tel<span><%= f.text_field :casehandlertel %></span></li>

<li>Case Handler Email<span><%= f.text_field :casehandleremail %></span></li>

<li>Claimant Name<span><%= f.text_field :claimantname %></span></li>

<li>Claimant Address<span><%= f.text_field :claimantaddress %></span></li>

<li>Claimant Contact<span><%= f.text_field :claimantcontact %></span></li>

<li>Claimant Tel<span><%= f.text_field :claimanttel %></span></li>

<li>Claimant Mob<span><%= f.text_field :claimantmob %></span></li>

<li>Claimant Email<span><%= f.text_field :claimantemail %></span></li>

<li>Claimant URL<span><%= f.text_field :claimanturl %></span></li>

<li>Comments<span><%= f.text_field :comments %></span></li>

</ul>

<div class="js_option">
<%= link_to_function "Show financial options.", 
"Element.show('finance_showhide');"
%>
</div>

<div id="finance_showhide" style="display:none">

<ul id="kases_new_finance">

<li>Invoice Number<span><%= f.text_field :invoicenumber %></span></li>
<li>Net Amount<span><%= f.text_field :netamount %></span></li>
<li>VAT<span><%= f.text_field :vat %></span></li>
<li>Gross Amount<span><%= f.text_field :grossamount %></span></li>
<li>Date Closed<span><%= f.date_select :dateclosed %></span></li>
<li>Date Paid<span><%= f.date_select :datepaid %></span></li>

</ul>

<div class="js_option">
<%= link_to_function "I'm confused! Hide financial options.", 
"Element.hide('finance_showhide');"
%>
</div>

</div>

<p>
<%= f.submit "Create" %>
</p> 
<% end %>

<%= link_to 'Back', kases_path %>

I have tried putting the

  • <%= f.file_field :avatar %>
  • in it's own form on the same page, but that didn't make a difference.


    EDIT: In Rails 3.1 :html => { :multipart => true } will be rendered in the form automatically once there's a file_field in your form.

    instead of

    <% form_for(@kase) do |f| %>
    

    use

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

    The content type "multipart/form-data" should be used for submitting forms that contain files.

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜