rails初心者です。
active_adminおよびcarrierwaveを使って管理画面で画像を投稿する機能を追加し、viewで表示しようとした際、以下のようなエラーが出てどのように解決すれば良いのかさっぱり分かりません。
ArgumentError in Studios#show Can't resolve image into URL: undefined method `to_model' for #<ImageUploader:0x00007f5c5c2a7c80> Did you mean? to_xml
このサイトを参考に作っていました。
Railsで簡単にブログを作る1(初心者向け)
ソースコード
post_image.rb
class PostImage < ApplicationRecord mount_uploader :image, ImageUploader belongs_to :studio end
studio.rb
class Studio < ApplicationRecord has_many :post_images accepts_nested_attributes_for :post_images validates :title, presence: true, length: { maximum: 50 } validates :tel, presence: true, length: { maximum: 50 } validates :date, presence: true, length: { maximum: 50 } validates :holiday, presence: true, length: { maximum: 50 } validates :address, presence: true, length: { maximum: 100 } validates :charge, presence: true, length: { maximum: 50 } validates :genre, presence: true, length: { maximum: 100 } validates :content, presence: true, length: { maximum: 500 } end
studios/show.html.erb
<%= image_tag(@studio.post_images[0].image) %>
image_uploder
class ImageUploader < CarrierWave::Uploader::Base include CarrierWave::RMagick storage :file def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end #上限変更 process :resize_to_limit => [700, 700] #JPGで保存 process :convert => 'jpg' #サムネイルを生成 version :thumb do process :resize_to_limit => [300, 300] end # jpg,jpeg,gif,pngのみ def extension_white_list %w(jpg jpeg gif png) end #ファイル名を変更し拡張子を同じにする def filename super.chomp(File.extname(super)) + '.jpg' end #日付で保存 def filename if original_filename.present? time = Time.now name = time.strftime('%Y%m%d%H%M%S') + '.jpg' name.downcase end end end
ちなみに管理者ページ内ではきちんと画像が表示されました。
admin/studo.rb
ActiveAdmin.register Studio do permit_params :title, :tel, :date, :holiday, :address, :charge, :genre, :content, post_images_attributes: [:id, :image, :reference, :_destroy] form do |f| f.inputs do f.input :title f.input :tel f.input :date f.input :holiday f.input :address f.input :charge f.input :genre f.input :content f.has_many :post_images, allow_destroy: true, new_record: true do |t| t.input :image end end f.actions end end
admin/post_images.rb
ActiveAdmin.register PostImage do #indexページ index do selectable_column id_column column :image do |b| image_tag(b.image.url(:thumb)) end column :post_id column :created_at column :updated_at actions end #showページ show do attributes_table do row :image do |b| image_tag(b.image.url(:thumb)) end row :created_at row :updated_at end end permit_params :image, :post_id end
あなたの回答
tips
プレビュー