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

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

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

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

Ruby on Rails 6

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

Ruby on Rails

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

Q&A

解決済

1回答

2624閲覧

NameError in Prototypes#index を解除したい

toratail

総合スコア12

Ruby

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

Ruby on Rails 6

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/10/13 14:18

トップページの画面に画像を表示させるためのコードを書いていたのですが、
NameError in Prototypes#index
Showing /Users/taigasoma/projects/protospace-30528/app/views/prototypes/_prototype.html.erb where line #2 raised:

uninitialized constant Prototype::Image

とエラー文が出てきました。

app > views > prototype > _prototype.html.erb <div class="card"> <%= link_to image_tag(prototype.image, class: :card__img ), prototype_path(prototype.id), method: :get %> <div class="card__body"> <%= link_to prototype.title, prototype_path(prototype.id), method: :get, class: :card__title%> <p class="card__summary"> <%= prototype.catch_copy %> </p> <%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %> </div> </div>

二行目の
<%= link_to image_tag(prototype.image, class: :card__img ), prototype_path(prototype.id), method: :get %>

にてNameErrorが発生しました。

index.htmlは

<main class="main"> <div class="inner"> <%# ログインしているときは以下を表示する %> <% if user_signed_in? %> <div class="greeting"> こんにちは、 <%= link_to "#{current_user.name}さん", "/users/#{current_user.id}", class: :greeting__link %> </div> <% end %> <%# // ログインしているときは上記を表示する %> <div class="card__wrapper"> <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %> <div class="prototypes"> <%= render partial: 'prototypes/prototype', collection: @prototypes %> </div> </div> </div> </main>

コントローラーは、

class PrototypesController < ApplicationController before_action :authenticate_user!, except: [:index, :show] def index @prototypes = Prototype.all 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 def show @prototype = Prototype.find(params[:id]) @comment = Comment.new @comments = @prototype.comments.includes(:user) end def edit @prototype =Prototype.find(params[:id]) end def update prototype = Prototype.find(params[:id]) if prototype.update(prototype_params) redirect_to prototype_path else render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end end

routes.rbは

Rails.application.routes.draw do devise_for :users root to: 'prototypes#index' resources :prototypes do resources :comments, only: :create end resources :users, only: :show end

modelsは

class Prototype < ApplicationRecord has_one_attached :image has_many :comments has_many :commnets, dependent: :destroy has_many :image, dependent: :destroy validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true belongs_to :user end

こちらが詳細のコードになります。
宜しくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

has_one_attached :imageを使っているので、has_many :image, dependent: :destroyは不要に思います

Active Storageの情報はこちらも参考になります。
Active Storage の概要

投稿2020/10/14 02:26

hatsu

総合スコア1809

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問