前提・実現したいこと
railsで投稿機能の実装しております
その中で、投稿者の名前を表示させたくその際、
以下のエラーメッセージが発生しました
発生している問題・エラーメッセージ
NoMethodError in Prototypes#index undefined method `name' for #<Prototype:0x00007fc0223cc488>
該当のソースコード
_prototype.html.erb
<div class="card"> <%= link_to prototype_path(prototype.id),method: :get do %> <%= image_tag prototype.image.variant(resize: '300x300'), class: :card__img if prototype.image.attached? %> <% end %> <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.name}", ←ここの部分です root_path, class: :card__user %> </div> </div>
prototypes_controller.rb
class PrototypesController < ApplicationController 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(@prototype) else @prototype = @prototype.includes(:user) render :new end end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept,:image).merge(user_id: current_user.id) end def show @prototype = Prototype.find(params[:id]) end end ``` routes.rb ``` Rails.application.routes.draw do devise_for :users root to: 'prototypes#index' resources :prototypes, only: [:index, :new, :create,:show] # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end ``` ### 試したこと 1userと紐づいてないと思い、 アソシエーションを確認しました userモデル has_many :prototypes prototypeモデル belongs_to :user は記述されていました ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/10/24 04:57