ハロー
Railsバージョン2.3.5
私はレールを学んでいて、問題にぶつかります。
railscastsチュートリアルからいくつかのネストフォームを実行しています。写真をアップロードするためにテキスト領域をデータフィールドに変更しましたが、すべてが機能しています。
今、私はアップロードされた写真を表示する必要があります、そして私は単にそれをすることができません。ネット上で見つけたものをすべて試しましたが、何も機能しませんでした。
問題
記事CRUDを処理するArticleコントローラーがあります。記事の新しいフォーム内には、画像をアップロードするためのフォームがネストされています。
記事コントローラー
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
写真モデル
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" %>
ありがとう