ruby on rails photo upload problem

Posted by dodo00700 on Stack Overflow See other posts from Stack Overflow or by dodo00700
Published on 2010-03-28T19:18:19Z Indexed on 2010/03/28 19:23 UTC
Read the original article Hit count: 176

Filed under:
|
|
|
|

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.error_messages %>

<%= 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" %>

<% end %>

Thanks

© Stack Overflow or respective owner

Related posts about rails

Related posts about photo