ruby on rails photo upload problem
Hallo
rails version 2.3.5
I'm learning rails and I run into a problem.
I'm doing some nesting forms from the railscasts tutorials. I changed the text area into a data field to upload photos and everything is working.
Now i have to display the uploaded pictures and i simply can't do it. I Tried everything I could find on the net but nothing worked.
PROBLEM
I have the Article controller which handles the article CRUD. inside the article new form there is nested a form for uploading images.
article controller
def code_image
@image_data = Photo.find(params[:id])
@image = @image_data.binary_data
send_data(@image, :type => @image_data.content_type,
:filename => @image_data.filename,
:disposition => 'inline')
end
photo model
def image_file=(input_data)
self.filename = input_data.original_filename
self.content_type = input_data.content_type.chomp
self.binary_data = input_data.read
end
articles/show.html.erb
<%=h @article.title %>
<%=h @article.body %>
<% for photos in @article.photos %>
<%= image_tag(url_for({:action => 'code_image',
:id => @article.photos.id})) -%>
<% end %>
articles/_formnew.html.erb
开发者_开发百科 <% form_for (:article, @article,
:url => {:action=>'create'}, :html=>
{:multipart=>true}) do |f| %>
<%= f.label :title %><br />
<%= f.text_field :title %><br /><br />
<%= f.label :body %><br />
<%= f.text_area :body, :style => 'width: 600px;' %><br /><br />
<% f.fields_for :photos do |builder|%>
<%= builder.label :content, "Photo"%><br />
<%= builder.file_field :image_file %><br />
<% end %>
<br />
<%= f.submit "Create" %>
Thanks
Old question I know, but if somebody stumbles across it, this page helped me: http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/
Also, to display the image, you can just use:
image_tag [model].[paperclip_attachment].url
eg:image_tag trip_image.photo.url
精彩评论