ある日突然
ActionController::UrlGenerationError in Api::Drinks#index
Showing /coffee_passport/app/views/api/drinks/index.json.jbuilder where line #2 raised:
No route matches {:action=>"show", :controller=>"active_storage/blobs/redirect", :filename=>nil, :format=>"json", :signed_id=>nil}, possible unmatched constraints: [:filename, :signed_id]
Extracted source (around line #2):
json.array! @drinks do |drink|
json.user_img rails_blob_url(drink.user.image) if drink.user.image
json.id drink.id
json.name drink.name
json.explain drink.explain
json.price drink.price
include SessionsHelper
def index
def index
@user = current_user
following_ids = 'SELECT followed_id FROM relationships WHERE follower_id = :user_id'
@drinks = Drink.includes(:user ,{image_attachment: :blob})
.where.not(user_id: 6)
binding.pry
.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: @user.id)
.order('drinks.created_at DESC')
end
end
しばらくリロードしてると、
NoMethodError (undefined method `id' for nil:NilClass):
となった、
@user
=> nil
[5] pry(#Api::DrinksController)> current_user
CACHE User Load (0.0ms) SELECT users
.* FROM users
WHERE users
.id
= 5 LIMIT 1
↳ app/helpers/sessions_helper.rb:52:in `current_user'
=> nil
案の定current_userが機能してない。。。
逆になぜ今までできてたのか、、、、
applicationControllerを継承してるはずだからモジュールのインポートできそうだけど、、、
include SessionsHelper
NoMethodError: undefined method `include' for #<Api::DrinksController:0x007f49aeb53f20>
from (pry):4:in `index'
とデバッグでは表示された。
今までできてたことがきゅうにできなくなるのはおかしい。。。
やっと分かった。
前回
seeds.rb
で
User.create!(
id: 5,
nickname: "ゲスト様",
email: "guest@example.com",
password: "password",
password_confirmation: "password",
activated: true,
activated_at: "Time.zone.now"
)
このように定義しました。なぜかというと
def index
def index
@user = current_user
following_ids = 'SELECT followed_id FROM relationships WHERE follower_id = :user_id'
@drinks = Drink.includes(:user ,{image_attachment: :blob})
.where.not(user_id: 6)
binding.pry
.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: @user.id)
.order('drinks.created_at DESC')
end
end
といった方法で定義してましたが、
これだと、ユーザーがフォローも投稿もしてないと、トップページが寂しくなるからです。
そうなると面接官さんに落とされてしまします。
なので、
User.create!(
id: 6,
nickname: "出品ユーザー",
email: "harasou21soccer@gmail.com",
password: ENV['ADMIN_USER_PASSWORD'],
password_confirmation: ENV['ADMIN_USER_PASSWORD'],
activated: true,
activated_at: "Time.zone.now"
)
# ユーザーは差し込めたので一旦コメントアウト
User.create!(
id: 1,
nickname: "はらそう",
email: "soccer@gmail.com",
password: "password",
password_confirmation: "password",
activated: true,
activated_at: "Time.zone.now"
)
User.create!(
id: 2,
nickname: "ジェファーソン",
email: "socce2r@gmail.com",
password: "password",
password_confirmation: "password",
activated: true,
activated_at: "Time.zone.now"
)
User.create!(
id: 5,
nickname: "ゲスト様",
email: "guest@example.com",
password: "password",
password_confirmation: "password",
activated: true,
activated_at: "Time.zone.now"
)
Drink.create!(
name: "ケニア",
price: 1050,
explain: " #RICH #LIVELY ナッツやココアの味わいが特徴で、 ローストによって引き出された香りやコクなど、すべてのバランスがよいコーヒー。",
user_id: 1,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/houseblend.jpg"),filename: "houseblend.jpg")
)
Drink.create!(
name: "スマトラ",
price: 1110,
explain: " #コクが有る ハーブのような香りがして、コクがあっておいしい。",
user_id: 2,
region_id: 3,
body_id: 4,
acidity_id: 4,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/sumatora.jpg"),filename: "sumatora.jpg")
)
Drink.create!(
name: "カプチーノ",
price: 1110,
explain: " #コクが有る ハーブのような香りがして、コクがあっておいしい。",
user_id: 2,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/cappuccino.jpg"),filename: "cappuccino.jpg")
)
Drink.create!(
name: "ヨーロピアンブレンド",
price: 350,
explain: " #バランスがいい 非常にバランスが良くてとてもおいしい",
user_id: 1,
region_id: 2,
body_id: 2,
acidity_id: 2,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/europe.jpg"),filename: "europe.jpg")
)
houseblend = Drink.create!(
name: "ハウスブレンド",
price: 1050,
explain: " #RICH #LIVELY ナッツやココアの味わいが特徴で、スターバックス ローストによって引き出された香りやコクなど、すべてのバランスがよいコーヒー。",
user_id: 6,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/houseblend.jpg"),filename: "houseblend.jpg")
)
# houseblend.image.attach(io: File.open(Rails.root.join("app/assets/images/houseblend.jpeg")),filename: "houseblend.jpeg")
Relationship.create!(
followed_id: 1,
follower_id: 5
)
Relationship.create!(
followed_id: 5,
follower_id: 1
)
Relationship.create!(
followed_id: 2,
follower_id: 5
)
Relationship.create!(
followed_id: 5,
follower_id: 2
)
このようにサンプルデータを作りました。
def new_guest
user = User.find_or_create_by!(id: 5,nickname: 'ゲスト様', email: 'guest@example.com') do |user|
# guest@example.com
# って登録されたらバリデーション エラーが起こる
# 先にゲストは登録しておく必要がある
user.password = "password"
# user.confirmed_at = Time.now # Confirmable を使用している場合は必要
end
log_in user
redirect_to root
end
ここでゲストログインをできますが、
最初にseedでゲストユーザーを定義しないと
rails db:seedをしたときに
Relationship.create!(
followed_id: 5,
follower_id: 2
)
でエラーが起きてしまいました。
なんか色々ややこしい
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
# 出品ユーザーを定義
User.create!(
id: 6,
nickname: "出品ユーザー",
email: "harasou21soccer@gmail.com",
password: ENV['ADMIN_USER_PASSWORD'],
password_confirmation: ENV['ADMIN_USER_PASSWORD'],
activated: true,
activated_at: "Time.zone.now"
)
# ユーザーは差し込めたので一旦コメントアウト
User.create!(
id: 1,
nickname: "はらそう",
email: "soccer@gmail.com",
password: "password",
password_confirmation: "password",
activated: true,
activated_at: "Time.zone.now"
)
User.create!(
id: 2,
nickname: "ジェファーソン",
email: "socce2r@gmail.com",
password: "password",
password_confirmation: "password",
activated: true,
activated_at: "Time.zone.now"
)
Drink.create!(
name: "ケニア",
price: 1050,
explain: " #RICH #LIVELY ナッツやココアの味わいが特徴で、 ローストによって引き出された香りやコクなど、すべてのバランスがよいコーヒー。",
user_id: 1,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/houseblend.jpg"),filename: "houseblend.jpg")
)
Drink.create!(
name: "スマトラ",
price: 1110,
explain: " #コクが有る ハーブのような香りがして、コクがあっておいしい。",
user_id: 2,
region_id: 3,
body_id: 4,
acidity_id: 4,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/sumatora.jpg"),filename: "sumatora.jpg")
)
Drink.create!(
name: "カプチーノ",
price: 1110,
explain: " #コクが有る ハーブのような香りがして、コクがあっておいしい。",
user_id: 2,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/cappuccino.jpg"),filename: "cappuccino.jpg")
)
Drink.create!(
name: "ヨーロピアンブレンド",
price: 350,
explain: " #バランスがいい 非常にバランスが良くてとてもおいしい",
user_id: 1,
region_id: 2,
body_id: 2,
acidity_id: 2,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/europe.jpg"),filename: "europe.jpg")
)
houseblend = Drink.create!(
name: "ハウスブレンド",
price: 1050,
explain: " #RICH #LIVELY ナッツやココアの味わいが特徴で、スターバックス ローストによって引き出された香りやコクなど、すべてのバランスがよいコーヒー。",
user_id: 6,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/houseblend.jpg"),filename: "houseblend.jpg")
)
# houseblend.image.attach(io: File.open(Rails.root.join("app/assets/images/houseblend.jpeg")),filename: "houseblend.jpeg")
# 複数の書き方はこんな感じ
Drink.create!(
[
{
name: "ブレックファーストブレンド",
price: 1050,
explain: " #BRIGHT(鮮やかな) #TANGY(はじけるような) やや浅めに焙煎された、軽めながらほどよいコクのブレンド。鮮やかでシトラス感とさわやかな後味が特徴。一日の始まりにふさわしい、いきいきとしたコーヒーです。",
user_id: 6,
region_id: 3,
body_id: 2,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/break_first.jpg"),filename: "breake_first.jpg")
},
{
name: "パイクプレイス® ロースト",
price: 1140,
explain: " #SMOOTH(なめらかな) #BALANCED(バランスの良い) ココアや煎ったナッツのようなほのかな香ばしさに、バランスのとれたなめらかな口あたりが特徴のコーヒー。一日を通して、また毎日でもお楽しみいただけます。",
user_id: 6,
region_id: 3,
body_id: 3 ,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/pike_place.jpg"),filename: "pike_place.jpg")
},
{
name: "グアテマラ アンティグア",
price: 1240,
explain: " #COCOA(ココアのような) #SUBTLE_SPICE(微かなスパイス感) チョコレートやかすかなスパイス、レモンを感じる上品な酸味が幾重にも重なる複雑ながらもエレガントで洗練されたコーヒー。火山に囲まれた100年もの歴史を誇るグアテマラ アンティグア地方の農園は、すばらしいコーヒーを栽培する生産地としてよく知られています。",
user_id: 6,
region_id: 3,
body_id: 3 ,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/guatemala.jpg"),filename: "guatemala.jpg")
},
{
name: "エチオピア",
price: 1330,
explain: " #CITRUS(シトラス) #DARK_COCOA(ダークココア) ダークチョコレート、ペッパーのようなスパイス、そしてスイートシトラスの風味が特徴の、やわらかでベルベットのような口あたりのコーヒーです。",
user_id: 6,
region_id: 4,
body_id: 3 ,
acidity_id: 4,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/ethiopia.jpg"),filename: "ethiopia.jpg")
},
{
name: "TOKYOロースト",
price: 1240,
explain: " #Hearty(心温まる) #Well-Rounded(まろやかな) スターバックス リザーブ® ロースタリー 東京のロースターが焙煎した、なめらかで深みのあるスマトラ産コーヒーにラテンアメリカ産コーヒーをブレンドした、まろやかで心温まるコーヒーです。",
user_id: 6,
region_id: 2,
body_id: 4 ,
acidity_id: 3,
processing_id: 5,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/tokyo_roast.jpg"),filename: "tokyo_roast.jpg")
}
]
)
んで、ブラウザでゲストログインして
わざわざ
/user/1と 入力してふぉろーして
/user/2と入力してフォローして
って感じにすれば
うまくいく。
Relationshipのseedの定義が上手く行かないい。
最初にユーザー作らないとだめなんかな
ActionController::UrlGenerationError in Api::Drinks#index
Showing /coffee_passport/app/views/api/drinks/index.json.jbuilder where line #2 raised:
No route matches {:action=>"show", :controller=>"active_storage/blobs/redirect", :filename=>nil, :format=>"json", :signed_id=>nil}, possible unmatched constraints: [:filename, :signed_id]
Extracted source (around line #2):
json.array! @drinks do |drink|
json.user_img rails_blob_url(drink.user.image) if drink.user.image
json.id drink.id
json.name drink.name
json.explain drink.explain
json.price drink.price
したらこのエラー。
きっとcurrent_userが上手くいってない。。。
[3] pry(#<Api::DrinksController>)> @user
=> #<User:0x00007f03a657a228
id: 5,
nickname: "ゲスト様",
email: "guest@example.com",
created_at: Mon, 30 Aug 2021 16:46:19.759349000 UTC +00:00,
updated_at: Mon, 30 Aug 2021 16:51:07.279598000 UTC +00:00,
password_digest: [FILTERED],
remember_digest: nil,
activation_digest: "$2a$12$jQ8SUboRaAxSRFiSUW7gMOTMP37M34hhRfaYxIR2SH3KnQRm4T22q",
activated: false,
activated_at: nil>
@userは問題なし
ん、じゃあ急にできなくなったのは、なんで。。。
ActionController::UrlGenerationError in Api::Drinks#index
Showing /coffee_passport/app/views/api/drinks/index.json.jbuilder where line #2 raised:
No route matches {:action=>"show", :controller=>"active_storage/blobs/redirect", :filename=>nil, :format=>"json", :signed_id=>nil}, possible unmatched constraints: [:filename, :signed_id]
migrate:resetして、1からやり直したら
soichirohara@SoichironoMacBook-Pro coffee_passport % docker-compose exec web rails db:seed
rails aborted!
ActiveRecord::RecordInvalid: バリデーションに失敗しました: Userを入力してください
/coffee_passport/db/seeds.rb:46:in <main>' /coffee_passport/bin/rails:9:in
'
/coffee_passport/bin/spring:15:in `'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
とエラーがでた。やはり、user_id:5の部分がまずかった。
おんなじとこつまずいてるひといた
てかマイグレーション書いたあたりからややこしくなったので、一旦やり直し
drinks#destroy
api_drinks GET /api/drinks(.:format) api/drinks#index {:format=>/json/}
pathも正常のように思える
分かった。
多分jsonのデータがなにもないから
ActionController::UrlGenerationError
とエラーが起こる
対処法として、最初からゲストユーザーの投稿のダミーデータを入れたかったが、
seedでゲストユーザーを定義すると、
ゲストユーザーとしてログインするときに
バリデーションエラーが起こる
もしくは、app.vueでaxios.getしたデータが無かった場合の処理をおこなうか。
def new_guest
user = User.find_or_create_by!(nickname: 'ゲスト様', email: 'guest@example.com') do |user|
# guest@example.com
# って登録されたらバリデーション エラーが起こる
# 先にゲストは登録しておく必要がある
user.password = SecureRandom.urlsafe_base64
# user.confirmed_at = Time.now # Confirmable を使用している場合は必要
end
log_in user
redirect_to root_path
end
最初にseedでゲストユーザーを定義して、
user.findだったらいけるんじゃね。しらんけど。。。
def new_guest
user = User.find_by(id:5,nickname: 'ゲスト様', email: 'guest@example.com')
# guest@example.com
# って登録されたらバリデーション エラーが起こる
# 先にゲストは登録しておく必要がある
# user.confirmed_at = Time.now # Confirmable を使用している場合は必要
log_in user
redirect_to root_path
end
User.create!(
id: 5,
nickname: "ゲスト様",
email: "guest@example.com",
password: "password",
password_confirmation: "password",
activated: true,
activated_at: "Time.zone.now"
)
Drink.create!(
name: "ケニア",
price: 1050,
explain: " #RICH #LIVELY ナッツやココアの味わいが特徴で、 ローストによって引き出された香りやコクなど、すべてのバランスがよいコーヒー。",
user_id: 5,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/houseblend.jpg"),filename: "houseblend.jpg")
)
Drink.create!(
name: "スマトラ",
price: 1110,
explain: " #コクが有る ハーブのような香りがして、コクがあっておいしい。",
user_id: 5,
region_id: 3,
body_id: 4,
acidity_id: 4,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/sumatora.jpg"),filename: "sumatora.jpg")
)
Drink.create!(
name: "カプチーノ",
price: 1110,
explain: " #コクが有る ハーブのような香りがして、コクがあっておいしい。",
user_id: 5,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/cappuccino.jpg"),filename: "cappuccino.jpg")
)
Drink.create!(
name: "ヨーロピアンブレンド",
price: 350,
explain: " #バランスがいい 非常にバランスが良くてとてもおいしい",
user_id: 5,
region_id: 2,
body_id: 2,
acidity_id: 2,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/europe.jpg"),filename: "europe.jpg")
)
houseblend = Drink.create!(
name: "ハウスブレンド",
price: 1050,
explain: " #RICH #LIVELY ナッツやココアの味わいが特徴で、スターバックス ローストによって引き出された香りやコクなど、すべてのバランスがよいコーヒー。",
user_id: 5,
region_id: 3,
body_id: 3,
acidity_id: 3,
processing_id: 2,
image: ActiveStorage::Blob.create_and_upload!(io: File.open("app/assets/images/houseblend.jpg"),filename: "houseblend.jpg")
)
def index
def index
@user = current_user
#binding.pry
following_ids = 'SELECT followed_id FROM relationships WHERE follower_id = :user_id'
@drinks = Drink.all.where.not(user_id: 6)
# includes(:user ,{image_attachment: :blob})
# .where.not(user_id: 6)
# .where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: @user.id)
# .order('drinks.created_at DESC')
end
end
def new_guest
user = User.find_by(id:5,nickname: 'ゲスト様', email: 'guest@example.com')
# guest@example.com
# って登録されたらバリデーション エラーが起こる
# 先にゲストは登録しておく必要がある
# user.confirmed_at = Time.now # Confirmable を使用している場合は必要
log_in user
redirect_to root_path
end
それで、ゲストユーザーの画像をちゃんと設定すればいけた。。。。
最初からseedで設定しよ。。。
そしてフォローとかのリレーションも組もう。
userの画像を設定してないからjsonでエラーが起きてそうなので、、、
seedでuserの画像をしっかり定義するか。
userの画像がなくても大丈夫なようにしたい。
後置if文をかいてもだめだったので、
json.array! @drinks do |drink|
if drink.user.image
json.user_img rails_blob_url(drink.user.image)
end
このように書いてもだめだった。。。
ユーザーの画像がなくても大丈夫なような設計にしたいので、、、
てか、こんかいはユーザーの画像がないときに
jsonがエラーを出してるのがすべての元凶な気がしてきた。
if drink.user.image.present?
json.user_img rails_blob_url(drink.user.image)
end
<div v-if="drink.user_img">
<img class="user-img-timeline" v-bind:src="drink.user_img" >
</div>
<div v-else>
<img class="user-img-timeline" src ="https://images.unsplash.com/photo-1469334031218-e382a71b716b?ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8YnJlYXV0aWZ1bCUyMGdpcmx8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=700&q=60">
</div>
こんな感じにした。
したら、画像なしのユーザーがいても大丈夫だった!!!
localhost:3000/api/drinksにアクセスしたら
[{"id":15,"name":"テスト","explain":"いいね","price":1000,"acidity_id":4,"body_id":4,"processing_id":3,"region_id":4,"region_name":"アフリカ","acidity_name":"HIGH(強い)","body_name":"FULL(しっかり)","processing_name":"SEMI-WASHED(半水洗式)","likes_count":null,"user_id":7,"nickname":"画像なしユーザー","image":"http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--ba4802b60c9b49f1b889894edb71eba2b82ea237/anastasiia-chepinska-lcfH0p6emhw-unsplash.jpg"},{"user_img":"http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--02f73b6da180966009d7661377d60b7db050e227/guest.png","
画像を設定してないユーザーの画像はなかった。
jsonのifがちゃんと機能してる、、、、???
binding.pryで検証するか
[3] pry(#<#<Class:0x00007fb7e8189258>>)> drink
=> #<Drink:0x00007fb7e81c3548
id: 15,
name: "テスト",
price: 1000,
explain: "いいね",
user_id: 7,
created_at: Tue, 31 Aug 2021 07:51:47.517091000 UTC +00:00,
updated_at: Tue, 31 Aug 2021 07:51:47.583297000 UTC +00:00,
region_id: 4,
body_id: 4,
acidity_id: 4,
processing_id: 3,
likes_count: nil>
[4] pry(#<#<Class:0x00007fb7e8189258>>)> drink.user.image
=> #<ActiveStorage::Attached::One:0x00007fb7e9043be0
@name="image",
@record=
#<User:0x00007fb7dd8a4868
id: 7,
nickname: "画像なしユーザー",
画像なしユーザーの画像がある。。。???
ゲストユーザーの画像をなしでもっかいやるか。。。
「exit!・exit-program・!!!・disable-pry」
ループ処理のなかでbinding.pryをやると、めんどいので、
こーゆーのがあるらしい。
[{"id":16,"name":"d","explain":"d","price":3000,"acidity_id":1,"body_id":1,"processing_id":1,"region_id":1,"region_name":"---","acidity_name":"---","body_name":"---","processing_name":"---","likes_count":null,"user_id":8,"nickname":"gs","image":"http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--8e9c04734b7d859d0ad50198b37adf0f7dfe6d29/ben-tofan-aQlSRcKHIfA-unsplash.jpg"}]
再度画像なしのユーザーで投稿して、api/drinksでアクセスした。
んで、みたらuser_imgはない。ってことでif文が機能してると信じたい。が
binding.pryで
dirnk.user.imageとしたらnilではなく、active strageオブジェクトが表示される
[5] pry(#<#<Class:0x00007fb7e1180470>>)> drink.user.image
=> #<ActiveStorage::Attached::One:0x00007fb7e81f0d40
@name="image",
@record=
#<User:0x00007fb7e11c1420
なんとも不思議。。。
app.vueで
<div v-if="drink.user_img">
<img class="user-img-timeline" v-bind:src="drink.user_img" >
</div>
<div v-else>
<img class="user-img-timeline" src ="https://images.unsplash.com/photo-1469334031218-e382a71b716b?ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8YnJlYXV0aWZ1bCUyMGdpcmx8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=700&q=60">
</div>
こうやってるおかげなのか。。。
でも、
created(){
this.setDrink();
},
としてる。
だから意味分かんない。。。
まぁ多分これでユーザーの画像を設定してなくてもエラーは起きないと信じたい。。
改めてここまでの疑問点
ActionController::UrlGenerationError in Api::Drinks#index
Showing /coffee_passport/app/views/api/drinks/index.json.jbuilder where line #2 raised:
No route matches {:action=>"show", :controller=>"active_storage/blobs/redirect", :filename=>nil, :format=>"json", :signed_id=>nil}, possible unmatched constraints: [:filename, :signed_id]
Extracted source (around line #2):
とエラーがでた。
色々分析した結果。
userの画像がないから
drink.user.imageでエラーが起きてた。
後置if文を書いてたはずだが、このように書いてみた。
json.array! @drinks do |drink|
#binding.pry
if drink.user.image.present?
json.user_img rails_blob_url(drink.user.image)
end
<div v-if="drink.user_img">
<img class="user-img-timeline" v-bind:src="drink.user_img" >
</div>
<div v-else>
<img class="user-img-timeline" src ="https://images.unsplash.com/photo-1469334031218-e382a71b716b?ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8YnJlYXV0aWZ1bCUyMGdpcmx8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=700&q=60">
</div>
このように書いてみた。
そうしたら先程のエラーは起きなくなった。
しかし疑問が残るのが、
画像なしのユーザーで登録して、投稿して
localhost:3000/api/drinks
でアクセスしてjsonデータを見てみたら
[{"id":16,"name":"d","explain":"d","price":3000,"acidity_id":1,"body_id":1,"processing_id":1,"region_id":1,"region_name":"---","acidity_name":"---","body_name":"---","processing_name":"---","likes_count":null,"user_id":8,"nickname":"gs","image":"http://localhost:3000/rails/active_storage/blobs/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--8e9c04734b7d859d0ad50198b37adf0f7dfe6d29/ben-tofan-aQlSRcKHIfA-unsplash.jpg"}]
このように表示されていて、user_imgはない。
つまり、投稿してるユーザーの画像はない。
しっかりとjbuilderのif文が上手く機能してるはず。
json.array! @drinks do |drink|
binding.pry
if drink.user.image.present?
json.user_img rails_blob_url(drink.user.image)
end
で、drink.user.imageと打つと、
=> #<ActiveStorage::Attached::One:0x00007fb7e84e5690
active strageオブジェクトっぽいものがある。
これがnilだったら話はわかるが、、、
<script>
created(){
this.setDrink();
},
methods: {
setDrink: function(){
axios.get('/api/drinks')
.then(response =>(
this.drinks = response.data
))
},
</script>
としてるので、
def index
def index
@user = current_user
following_ids = 'SELECT followed_id FROM relationships WHERE follower_id = :user_id'
@drinks = Drink.includes(:user ,{image_attachment: :blob})
.where.not(user_id: 6)
.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: @user.id)
.order('drinks.created_at DESC')
end
end
が作動して、
index.json.jbuilderが動いて、
app.vueにデータが返却されるので、
user_imgはnilになるはずだが、、、。。
ためしにapp.vueを
<!-- <div v-if="drink.user_img"> -->
<img class="user-img-timeline" v-bind:src="drink.user_img" >
<!-- </div> -->
<!-- <div v-else> -->
<!-- <img class="user-img-timeline" src ="https://images.unsplash.com/photo-1469334031218-e382a71b716b?ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8YnJlYXV0aWZ1bCUyMGdpcmx8ZW58MHx8MHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=700&q=60">
</div> -->
こんな感じにして絶対drink.user_imgを表示するようにしたらどうなるか。。。
問題なかった。。。。
[2] pry(#<#>)> drink.user.image
=> #<ActiveStorage::Attached::One:0x00007fb7e915dc38
ただ、同じくnilにはならない。。。
json.array! @drinks do |drink|
binding.pry
# if drink.user.image.present?
json.user_img rails_blob_url(drink.user.image)
# end
こうしてみたらどうなるか。。
ActionController::UrlGenerationError in Api::Drinks#index
Showing /coffee_passport/app/views/api/drinks/index.json.jbuilder where line #4 raised:
No route matches {:action=>"show", :controller=>"active_storage/blobs/redirect", :filename=>nil, :format=>"json", :signed_id=>nil}, possible unmatched constraints: [:filename, :signed_id]
先程のエラーが表示された。。
よかった。。。
<ActiveStorage::Attached::One:0x00007fb7e8d233c0
のなかみは nilってことで
いいのかしら。。
とりあえず、dirnk.user.imageがなくとも
json.array! @drinks do |drink|
if drink.user.image.present?
json.user_img rails_blob_url(drink.user.image)
end
があるから一安心ってことだね。