前提・実現したいこと
工程管理をメモできるアプリを作ってます。
views/memos/show.html.rbにuser情報を表示させようと思い、memosとusersテーブルの紐づけの実装中に下記のエラーが発生しました。
memosの情報とusersの情報を紐づけて、views/memos/show.html.rbに表示させる為にはどうすればいいでしょうか。
教えて頂けると幸いです。宜しくお願いします。
発生している問題・エラーメッセージ
error
1Processing by MemosController#show as HTML 2 Parameters: {"id"=>"3"} 3Completed 500 Internal Server Error in 21ms (ActiveRecord: 0.0ms | Allocations: 1383) 4 5 6 7NoMethodError (undefined method `find_by' for Memo:Module): 8 9app/controllers/memos_controller.rb:7:in `show'
該当のソースコード
######db/migrate/20210106215236_add_user_id_to_user.rb
ruby
1class AddUserIdToUser < ActiveRecord::Migration[6.0] 2 def change 3 add_column :users, :memo_id, :integer 4 end 5end
######db/schema.rb
ruby
1ActiveRecord::Schema.define(version: 2021_01_06_215236) do 2 3 # These are extensions that must be enabled in order to support this database 4 enable_extension "plpgsql" 5 6 create_table "memos", force: :cascade do |t| 7 t.string "product_name", limit: 30, null: false 8 t.string "order_number", limit: 6, null: false 9 t.datetime "delivery_date" 10 t.integer "quantity" 11 t.string "process" 12 t.text "remarks" 13 t.datetime "created_at", precision: 6, null: false 14 t.datetime "updated_at", precision: 6, null: false 15 t.integer "user_id" 16 end 17 18 create_table "users", force: :cascade do |t| 19 t.string "name", limit: 30, null: false 20 t.string "email", null: false 21 t.string "password_digest", null: false 22 t.datetime "created_at", precision: 6, null: false 23 t.datetime "updated_at", precision: 6, null: false 24 t.string "remember_digest" 25 t.integer "memo_id" 26 t.index ["email"], name: "index_users_on_email", unique: true 27 end 28end
######app/controllers/memos_controller
ruby
1class MemosController < ApplicationController 2 def index 3 @memos = Memo.all 4 end 5 6 def show 7 @memo = Memo.find_by(id: params[:id]) 8 @user = User.find_by(id: @memo.user_id) 9 end 10 11 def new 12 @memo = Memo.new 13 end 14 15 def create 16 @memo = Memo.new( 17 content: params[:content], 18 user_id: @current_user.id 19 ) 20 if @memo.save 21 flash[:success] = "「#{@memo.product_name}」を登録しました。" 22 redirect_to @memo 23 else 24 render 'memo/new' 25 end 26 end 27 28 def edit 29 @memo = Memo.find(params[:id]) 30 end 31 32 def update 33 memo = Memo.find(params[:id]) 34 memo.update!(memo_params) 35 flash[:success] = "「#{@memo.product_name}」を編集しました。" 36 redirect_to tasks_url 37 end 38 39 def destroy 40 memo = Memo.find(params[:id]) 41 memo.destroy 42 flash[:success] = "「#{@memo}」を削除しました。" 43 redirect_to tasks_url 44 end 45 46 private 47 48 def memo_params 49 params.require(:task).permit(:product_name, :order_number, 50 :delivery_date, :quantity, 51 :process, :remarks) 52 end 53end
######app/controllers/users_controller
ruby
1class UsersController < ApplicationController 2 before_action :logged_in_user, only: [:index, :edit, :update, :destroy] 3 before_action :correct_user, only: [:edit, :update] 4 before_action :admin_user, only: :destroy 5 6 def show 7 @user = User.find(params[:id]) 8 end 9 10 def new 11 @user = User.new 12 end 13 14 def create 15 @user = User.new(user_params) 16 if @user.save 17 log_in @user 18 flash[:success] = "登録が完了!" 19 redirect_to @user 20 else 21 render 'new' 22 end 23 end 24 25 def edit 26 @user = User.find(params[:id]) 27 end 28 29 def update 30 @user = User.find(params[:id]) 31 if @user.update_attributes(user_params) 32 flash[:success] = "Profile updated" 33 redirect_to @user 34 else 35 render 'edit' 36 end 37 end 38 39 def index 40 @users = User.paginate(page: params[:page]) 41 end 42 43 def destroy 44 User.find(params[:id]).destroy 45 flash[:success] = "User deleted" 46 redirect_to users_url 47 end 48 49 private 50 51 def user_params 52 params.require(:user).permit(:name, :email, :password, 53 :password_confirmation) 54 end 55 56 def logged_in_user 57 unless logged_in? 58 store_location 59 flash[:danger] = "Please log in." 60 redirect_to login_url 61 end 62 end 63 64 def correct_user 65 @user = User.find(params[:id]) 66 redirect_to(root_url) unless current_user?(@user) 67 end 68 69 def admin_user 70 redirect_to(root_url) unless current_user.admin? 71 end 72end
######app/views/memos/show.html.erb
html
1<% provide(:title, @memo.product_name) %> 2<nav class="row justify-content-end"> 3<%= link_to '製品一覧', memos_path, class: 'nav-link' %> 4 <div class="col-md-4"> 5 <table class= "table table table-hover"> 6 <tbody> 7 <tr><th>ID</th><td><%= @memo.id %></td></tr> 8 <tr><th>品名</th><td><%= @memo.product_name %></td></tr> 9 <tr><th>注番</th><td><%= @memo.order_number %></td></tr> 10 <tr><th>製造日</th><td><%= @memo.created_at %></td></tr> 11 <tr><th>納期</th><td><%= @memo.delivery_date %></td></tr> 12 <tr><th>手配数量</th><td><%= @memo.quantity %></td></tr> 13 <tr><th>作業工程</th><td><%= @memo.process %></td></tr> 14 <tr><th>備考</th><td><%= @memo.remarks %></td></tr> 15 <tr><th>更新日時</th><td><%= @memo.updated_at %></tr> 16 <td><%= link_to "編集", edit_memo_path, class: "btn btn-primary" %></td> 17 <td><%= link_to "削除", @memo, method: :delete, 18 data: { confirm: "「#{@memo.product_name}」を削除します。よろしいですか?" }, 19 class: 'btn btn-danger' %></td> 20 </tbody> 21 </table> 22 </div> 23 <%= @user.name %> 24</nav>
######app/models/user.rb
ruby
1class User < ApplicationRecord 2 attr_accessor :remember_token 3 before_save { self.email = email.downcase } 4 validates :first_name, presence: true, length: { maximum: 50 }, 5 uniqueness: { scope: :user_id} 6 7 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 8 validates :email, presence: true, length: { maximum: 255 }, 9 format: { with: VALID_EMAIL_REGEX }, 10 uniqueness: { case_sensitive: false } 11 12 has_secure_password 13 validates :password, presence: true, length: { minimum: 6 }, allow_nil: true 14 15 has_many :memos 16 17 class << self 18 def digest(string) 19 cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : 20 BCrypt::Engine.cost 21 BCrypt::Password.create(string, cost: cost) 22 end 23 24 def new_token 25 SecureRandom.urlsafe_base64 26 end 27 end 28 29 def remember 30 self.remember_token = User.new_token 31 update_attribute(:remember_digest, User.digest(remember_token)) 32 end 33 34 def authenticated?(remember_token) 35 return false if remember_digest.nil? 36 BCrypt::Password.new(remember_digest).is_password?(remember_token) 37 end 38 39 def forget 40 update_attribute(:remember_digest, nil) 41 end 42end
######app/models/memo.rb
ruby
1class Memo < ApplicationRecord 2 belongs_to :user 3end
試したこと
memosテーブルとuser_idの紐づけはできていると思ってます。
memos_controller.rbにuserテーブルを紐づける為の記述。
user.rb、mermo.rbにデータベースを関連させる為の記述。
補足情報(FW/ツールのバージョンなど)
ruby 2.7.1p83
Rails 6.0.3.4
Ubuntu 20.04 LTS