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

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

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

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

Q&A

解決済

2回答

2027閲覧

rails のNoMethodError in Users#showの解決法

teasy

総合スコア1

Ruby on Rails

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

0グッド

0クリップ

投稿2020/08/25 08:13

前提・実現したいこと

railsで本の投稿アプリを作成しておりまして、本の一覧画面などに投降者の画像も配置したいのですが、以下のようなエラーが発生しました。問題点をしていただきたいです。

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

undefined method `image_id' for #<User:0x00007f06e5efee88> Did you mean? image <% @user.books.each do |book| %> <tr class="new-table"> <td><span><%= attachment_image_tag book.user, :image, :fill, 50, 50, format: 'jpeg' %></span></td>/この部分が赤くなっています <td><span><%= link_to book.title, "/books/#{book.id}"%></span></td> <td><span><%= book.body %></span></td> </tr>

該当のソースコード

ruby

1app/views/users/show.html.erb 2 3 4 5<head> 6 <%= render 'root/header', root: @root %> 7</head> 8<body> 9 10<div class="flash"> 11<% flash.each do |key, value| %> 12 <%= content_tag(:div, value, class: "#{key}") %> 13<% end %> 14</div> 15 16 17<div class="container"> 18 <div class="row"> 19 <div class="col-lg-3" style="background-color: white"> 20 <h2>User show info</h2> 21 <% if @user.profile_image_id != nil %> 22 <%= attachment_image_tag @user, :image, :fill, 150, 150, format: 'jpeg' %> 23 <% else %> 24 <%= image_tag 'sample_img.gif', style: "height: 150px" %> 25 <% end %> 26 <table class="table table-hover"> 27 28 <tbody> 29 <tr> 30 <td>name</td> 31 <td><%= @user.name %></td> 32 </tr> 33 <tr> 34 <td>Introduction</td> 35 <td><%= @user.introduction %></td> 36 </tr> 37 </tbody> 38 </table> 39 40<% if current_user == @user %> 41<%= link_to(edit_user_path(current_user.id), class: "btn btn-default") do %> 42 <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span> 43<% end %> 44<% end %> 45 46<h1>New book</h1> 47 48<%= form_for(@book) do |f| %> 49 50 <p>Title</p> 51 <%= f.text_field :title , class: "edit-title"%> 52 53 <p>Body</p> 54 <%= f.text_area :body, class: "edit-body"%> 55 <br> 56 <%= f.submit 'Create Book', class: "btn btn-primary" %> 57 58<% end %> 59 60 </div> 61 <div class="col-lg-9"> 62<h1>Books</h1> 63 64 <table class="table table-striped"> 65 <thead> 66 <tr> 67 <th><P>image</P></th> 68 <th><P>title</P></th> 69 <th><P>opinion</P></th> 70 </tr> 71 </thead> 72 <tbody> 73 <% @user.books.each do |book| %> 74 <tr class="new-table"> 75 <td><span><%= attachment_image_tag book.user, :image, :fill, 50, 50, format: 'jpeg' %></span></td> 76 <td><span><%= link_to book.title, "/books/#{book.id}"%></span></td> 77 <td><span><%= book.body %></span></td> 78 </tr> 79 <% end %> 80 81 </tbody> 82 </table> 83 </div> 84 </div> 85</div>
users_controller.rb class UsersController < ApplicationController before_action :authenticate_user!, only: [:new, :index, :show] def index @users = User.all @book = Book.new end def show @user = User.find(params[:id]) @book = Book.new end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) @user.update(user_params) redirect_to user_path(@user) end def create book = Book.new(book_params) book.save redirect_to'/books/new' end def books @users = User.all @book = Book.new @book = Book.all end private def user_params params.require(:user).permit(:name, :introduction, :image) end end
user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :books has_many :book_images attachment :image validates :name, presence: true, uniqueness: true, length: {minimum: 2, maximum: 20} validates :introduction, length: {maximum: 50} end

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

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

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

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

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

naokit-dev

2020/08/25 08:22

`image_id`が未定義とのエラーですが そもそもソースコードの中で`image_id`がみつからないのですが 提示されている内容はあっていますでしょうか
teasy

2020/08/25 08:29

はいこれで正しいと思います。 私もこのimage_idがどこからきているのかがわからなくて混乱しています、
naokit-dev

2020/08/25 08:36

profile_image_id というのは何を意図されているのでしょうか?
teasy

2020/08/25 08:43

create_table "books", force: :cascade do |t| t.text "title" t.text "body" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "profile_image_id" t.integer "user_id" end create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" t.string "profile_image_id" t.text "introduction" t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["email"], name: "index_users_on_email", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end end ここのモデルに入れたカラム名です
guest

回答2

0

自己解決

attachment :profile_image

に変更したら動きました。

投稿2020/08/26 08:50

teasy

総合スコア1

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

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

0

attachment :image これに違和感があります。
これから 関連 image を探すために image_id を参照に行った感。

has_one_attached とか
has_many_attached とかならよく見ますが

投稿2020/08/25 09:57

winterboum

総合スコア23401

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問