ペーパークリップを使用するモデルを次のように変更しました。
class UsersPicture < ActiveRecord::Base
attr_accessible :user_id, :users_picture_id, :photo
belongs_to :user
has_attached_file :photo,
:url => "users_pictures/:attachment/:id/:basename.:extension",
:path => ":rails_root/users_pictures/:attachment/:id/:basename.:extension"
before_create :rename_file_name
def rename_file_name
extension = File.extname(photo_file_name).downcase
filename = File.basename(photo_file_name).downcase
timestamp = Time.now.to_i
name = Digest::MD5.hexdigest("#{filename}")
self.photo.instance_write(:file_name, "#{name}_#{timestamp}#{extension}")
end
end
before_create メソッドを挿入する前は、すべて問題ありませんでした。しかし今、私のログには次のように書かれています:
ActionView::Template::Error (users_pictures/photos/88/8b7474622cf5bbc140ed7defe1ae76a8_1351088476.jpeg isn't precompiled):
8: <p>
9: <b>Users picture file:</b>
10: <% if @users_picture.photo.exists? then %>
11: <%= image_tag @users_picture.photo.url %>
12: <% end %>
13: </p>
14:
app/views/users_pictures/show.html.erb:11:in `_app_views_users_pictures_show_html_erb__597237708__324410398'
app/controllers/users_pictures_controller.rb:18:in `show'
私は解決策を探していましたが、「image_tag」を使用することを常に覚えていました。しかし、私は現在それを使用していますか?もちろん、インデックスは正常に機能しますが、show メソッドは機能しません:/ 何を変更できますか? 編集: image_tag を削除すると、次のようになります: users_pictures/photos/88/8b7474622cf5bbc140ed7defe1ae76a8_1351088476.jpeg
そして、これも正しい道です。