前提・実現したいこと
現在railsを使って画像投稿機能を製作中です。こちらのサイト(https://workabroad.jp/tech/1118)を参考にしながら政策をしています。
placesというテーブルに:pictureというカラムをつけていて、そこに画像のデータを入れています。
発生している問題・エラーメッセージ
NoMethodError in Places#index undefined method `picture?' for nil:NilClass
該当のソースコード
<% if @places.picture? %>のところにundefined method `picture?' for nil:NilClassというエラーが出ます。
view
1<% if @places.picture? %> 2 <%= image_tag @place.picture.thumb.url %> 3<% end %> 4
controller
1def index 2 @places = Place.all 3end 4 5def new 6 @place = Place.new 7 end 8 9 def create 10 @place = Place.new(place_params) 11 if @place.save 12 redirect_to root_path 13 end 14 end 15 16 private 17 18 def place_params 19 params.require(:place).permit(:name, :latitude, :longitude, :comment, :picture) 20 end 21end
model
1class Place < ApplicationRecord 2 mount_uploader :picture, PictureUploader 3end 4
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/17 03:03
2018/10/17 03:18