適当にひな形作る
$ rails g scaffold photo image:binary
app/models/photo.rb
class Photo < ActiveRecord::Base attr_accessible :image, :picture_file def picture_file= (file) if file self.image = file.read end end end
app/controllers/photos_controller.rb
#追加 def picture @image = Photo.find(params[:id]) send_data(@image.image) end
config/routes.rb
#変更 resources :photos do member do get :picture end end
app/views/photos/_form.html.erb
#変更app/views/photos/index.html.erb<%= f.label :picture_file %>
<%= f.file_field :picture_file %>
<% @photos.each do |photo| %> <%= image_tag picture_photo_path(photo), :size => '30x30' %> #修正 <%= link_to 'Show', photo %> <%= link_to 'Edit', edit_photo_path(photo) %> <%= link_to 'Destroy', photo, method: :delete, data: { confirm: 'Are you sure?' } %> <% end %>
app/views/photos/show.html.erb
Image: <%= image_tag picture_photo_path(@photo), :size => '30x30' %> #修正
これでバイナリーデータで保存して、表示できるヘ(^o^)ノ
でもね… select_dateのおかげでレコード毎にselect文が─=≡Σ((( つ•̀ω•́)つ
うーん、なんか良い方法ないですかね?
0 件のコメント:
コメントを投稿