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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1909閲覧

部分テンプレートを使って、投稿内容を表示させたいのですが、できません。

dragon38

総合スコア1

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/02/03 03:27

前提・実現したいこと

ProtoSpaceというアプリの実装において、
部分テンプレートを使って投稿内容を表示しようとしたら、
以下のエラーが発生してしまい、以降実装が止まっています。

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

https://gyazo.com/c0e71c3e9dd523f9fb7b78d7a8d658be

NameError in Prototypes#index Showing /Users/shrine-dragon3839/projects/protospace-32405/app/views/prototypes/_prototype.html.erb where line #2 raised: undefined local variable or method `prototype' for #<#<Class:0x00007f86421f3310>:0x00007f8641a08600> Did you mean? @prototypes

該当のソースコード

app/views/prototypes/_prototype.html.erb <div class="card"> <%= link_to image_tag(prototype.image, class: :card__img ), root_path%> <div class="card__body"> <%= link_to prototype.title, root_path, class: :card__title%> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by #{prototype.user.name}", root_path, class: :card__user %> </div> </div> ※仮置きのリンクroot_pathはそのままで良い
app/views/prototypes/index.html.erb <main class="main"> <div class="inner"> <% if user_signed_in? %> <div class="greeting"> <%= "こんにちは、" %> <%= link_to "#{current_user.name}さん", root_path, class: :greeting__link%> </div> <% end %> <div class="card__wrapper"> <%= render partial: 'prototypes/prototype', collection: @prototypes %> </div> </div> </main> 仮置きのリンクroot_pathはそのままで良い

試したこと

①prototype.imageを@prototypes.imageに変更

→NoMethodError in Prototypes#index
undefined method `image'、となり、更にエラーを重ねてしまう。

②部分テンプレートの呼び出しにおいて、
<%= render partial: 'prototypes/prototype', collection: @prototypes %>、と変更

→Association named 'prototype' was not found on Prototype; perhaps you misspelled it?、となり、更にエラーを重ねてしまう。

③コントローラーのindexアクションにおいて、
@prototypes = Prototype.includes(:user)

の部分を、
prototypes = Prototype.includes(:user)
に変えるとエラーはなくなるが、部分テンプレートは表示できないままで、
根本的な解決にはならない。

補足情報(FW/ツールのバージョンなど)

###ルーティング

Rails.application.routes.draw do devise_for :users root 'prototypes#index' resources :prototypes, only: [:new, :create] end

###コントローラー

class PrototypesController < ApplicationController def index @prototypes = Prototype.includes(:prototype) end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path else render :new end end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end end

###モデル

class Prototype < ApplicationRecord belongs_to :user has_one_attached :image validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true end

###マイグレーションファイル

class CreatePrototypes < ActiveRecord::Migration[6.0] def change create_table :prototypes do |t| t.string :title, null: false t.text :catch_copy, null: false t.text :concept, null: false t.references :user, foreign_key: true t.timestamps end end end

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

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

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

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

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

guest

回答1

0

ベストアンサー

def index @prototypes = Prototype.all.includes(:user) end
<%= render @prototypes %>

これでどうでしょうか?

投稿2021/02/03 07:12

編集2021/02/03 07:34
退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問