質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2335閲覧

image_tagを使用時にActionView::SyntaxErrorInTemplate

Yoshi63-1020

総合スコア1

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/11/23 05:41

前提・実現したいこと

image_tagを使用して画像をトップページに表示しようとしたところ、
ActionView::SyntaxErrorInTemplateが解決できません。

発生している問題・エラーメッセージ

ActionView::SyntaxErrorInTemplate in PrototypesController#index
Encountered a syntax error while rendering template: check <div class="card"> <%= link_to image_tag (prototype.image, class: :card__img) prototype_path(@prototype.id) %> <div class="card__body"> <%= link_to 'prototype.title', prototype_path(@prototype.id), class: :card__title %> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by #{prototype.user.name}", prototype_path(@prototype.id), class: :card__user %> </div> </div>

エラーメッセージ

該当のソースコード

app/views/prototypes/_prototype.html.erb

Ruby

1<div class="card"> 2 <%= link_to image_tag (prototype.image, class: :card__img) prototype_path(@prototype.id) %> 3 <div class="card__body"> 4 <%= link_to 'prototype.title', prototype_path(@prototype.id), class: :card__title %> 5 <p class="card__summary"> 6 <%= prototype.catch_copy %> 7 </p> 8 <%= link_to "by #{prototype.user.name}", prototype_path(@prototype.id), class: :card__user %> 9 </div> 10</div>

app/controllers/prototypes_controller.rb

Ruby

1class PrototypesController < ApplicationController 2 def index 3 @prototypes = Prototype.all 4 end 5 6 def new 7 @prototype = Prototype.new 8 end 9 10 def create 11 @prototype = Prototype.new(prototype_params) 12 if @prototype.save 13 redirect_to root_path 14 else 15 render :new 16 end 17 end 18 19 def prototype_params 20 params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 21 end 22end

app/models/prototype.rb

Ruby

1class Prototype < ApplicationRecord 2 belongs_to :user 3 has_one_attached :image 4 5 validates :title, presence: true 6 validates :catch_copy, presence: true 7 validates :concept, presence: true 8 validates :image, presence: true 9end

app/views/prototypes/index.html.erb

Ruby

1<main class="main"> 2 <div class="inner"> 3 <% if user_signed_in? %> 4 <div class="greeting"> 5 こんにちは、 6 <%= link_to "#{current_user.name}さん", root_path, class: :greeting__link%> 7 </div> 8 <% end %> 9 <div class="card__wrapper"> 10 <%= render 'prototype', collection: @prototypes %> 11 </div> 12 </div> 13</main>

config/routes.rb

Ruby

1Rails.application.routes.draw do 2 devise_for :users 3 root to: 'prototypes#index' 4 resources :prototypes, only: [:index, :new, :create, :show] 5end

試したこと

アソシエーションが間違っているために、prototype.imageで取得できていないのだと思い、アソシエーションの見直しを行いました。
link_toやimage_tagの使用方法も見直しましたが間違いがわかりません。

宜しくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

エラーメッセージのとおりです。

erb

1<%= link_to image_tag (prototype.image, class: :card__img) prototype_path(@prototype.id) %>

この箇所が文法的に成立していません。おそらくは以下のような形ではないでしょうか。

erb

1<%= link_to image_tag(prototype.image, class: :card__img), prototype_path(@prototype.id) %>

投稿2020/11/23 06:04

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Yoshi63-1020

2020/11/23 06:25

ありがとうございます。 追加したところ次はnameErrorが出てしまいました。undefined local variable or method `prototype'とあったのでindex.html.erbの<%= render 'prototype', collection: @prototypes %>の部分を <%= render 'prototype', collection: @prototypes, locals: { prototype: prototype } %>と変更しました。しかしエラーは解消されていません。もし理由がわかるようでしたら教えていただけたらありがたいです。
maisumakun

2020/11/23 06:28

上に書いたlink_toのの中のprototypeを@prototypeにしないといけない、ということではないでしょうか?
Yoshi63-1020

2020/11/23 06:39

度々申し訳ありません。 <%= link_to image_tag(@prototype.image, class: :card__img), prototype_path(@prototype.id) %>としたところ次はNoMethodError undefined method `image' となってしまいました。
Yoshi63-1020

2020/11/23 07:28

解決できました、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問