前提・実現したいこと
ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
簡単な投稿アプリを作っています。 デバイスを使って実装していました。その際に投稿した内容とNoMethodError in Posts#indexと出ました。
該当のソースコード
ruby
1.main 2 - @posts.each do |post| 3 .texts 4 .texts__header 5 .texts__header__user 6 = post.user.nickname 7 .texts__header__box 8 = link_to "Memori", post_path(post), class: 'btn btn-default' 9 = link_to "Edit", edit_post_path(post) , class: 'btn btn-default' 10 .texts__main 11 .texts__main__concreate 12 embodiment 13 .texts__main__concreate-md 14 = post.embodiment 15 .texts__main__abstract 16 Abstract 17 .texts__main__abstract-mb 18 = post.abstraction 19 .texts__main__action 20 My Action 21 .texts__main__action-mb 22 = post.action 23
Ruby
1class PostsController < ApplicationController 2 before_action :move_to_index, except: [:index, :show] 3 def index 4 @posts=Post.includes(:user).order("created_at DESC").page(params[:page]).per(3) 5 # @posts=Post.includes(:user) 6 end 7 8 def new 9 @post = Post.new 10 end 11 12 def create 13 binding.pry 14 Post.create(post_params) 15 redirect_to root_path 16 end 17 18 def show 19 @post = Post.find(params[:id]) 20 end 21 22 def edit 23 @post = Post.find(params[:id]) 24 end 25 26 def update 27 post = Post.find(params[:id]) 28 post.update(post_params) 29 redirect_to root_path(post) 30 end 31 32 def destroy 33 post = Post.find(params[:id]) 34 post.destroy 35 end 36 37 38 private 39 40 def post_params 41 params.require(:post).permit(:embodiment,:abstraction,:action,).merge(user_id: current_user.id) 42 end 43 44 # def id_params 45 # params.require(:post).permit(:id) 46 # end 47 48 def move_to_index 49 redirect_to action: :index unless user_signed_in? 50 end 51 52end 53
ruby
1class Post< ApplicationRecord 2 belongs_to :user 3end
ruby
1# Include default devise modules. Others available are: 2 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 3 devise :database_authenticatable, :registerable, 4 :recoverable, :rememberable, :validatable 5 validates :nickname, presence: true 6 has_many :posts 7end
ruby
1# frozen_string_literal: true 2 3class DeviseCreateUsers < ActiveRecord::Migration[5.2] 4 def change 5 create_table :users do |t| 6 ## Database authenticatable 7 t.string :nickname, null: false 8 t.string :email, null: false, default: "" 9 t.string :encrypted_password, null: false, default: "" 10 11 ## Recoverable 12 t.string :reset_password_token 13 t.datetime :reset_password_sent_at 14 15 ## Rememberable 16 t.datetime :remember_created_at 17 18 ## Trackable 19 # t.integer :sign_in_count, default: 0, null: false 20 # t.datetime :current_sign_in_at 21 # t.datetime :last_sign_in_at 22 # t.string :current_sign_in_ip 23 # t.string :last_sign_in_ip 24 25 ## Confirmable 26 # t.string :confirmation_token 27 # t.datetime :confirmed_at 28 # t.datetime :confirmation_sent_at 29 # t.string :unconfirmed_email # Only if using reconfirmable 30 31 ## Lockable 32 # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 33 # t.string :unlock_token # Only if unlock strategy is :email or :both 34 # t.datetime :locked_at 35 36 37 t.timestamps null: false 38 end 39 add_index :users, :nickname, unique: true 40 add_index :users, :email, unique: true 41 add_index :users, :reset_password_token, unique: true 42 # add_index :users, :confirmation_token, unique: true 43 # add_index :users, :unlock_token, unique: true 44 end 45end 46
ruby
1class AddIntroductionToUsers < ActiveRecord::Migration[5.2] 2 def change 3 add_column :users, :introduction, :text 4 end 5end
ruby
1class AddUserIdToPosts < ActiveRecord::Migration[5.2] 2 def change 3 add_column :posts, :user_id, :integer 4 end 5end 6
class AddNicknameToUsers < ActiveRecord::Migration[5.2] def change end end
試したこと
スペルミスの確認
ターミナルのログを見たそしたらCompleted 500 Internal Server Errorとでていました。
マイグレーションファイルをrollbackをしてやり直したが何も変化がない。
app/views/posts/index.html.haml:29:in `block in _app_views_posts_index_html_haml__636240355849085160_70171593036600' app/views/posts/index.html.haml:25:in `_app_views_posts_index_html_haml__636240355849085160_70171593036600' Started GET "/" for ::1 at 2020-02-24 20:15:36 +0900 Processing by PostsController#index as HTML User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 ↳ /Users/kazuto/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98 Rendering posts/index.html.haml within layouts/application Post Load (0.3ms) SELECT `posts`.* FROM `posts` ORDER BY created_at DESC LIMIT 3 OFFSET 0 ↳ app/views/posts/index.html.haml:25 User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ↳ app/views/posts/index.html.haml:25 Rendered posts/index.html.haml within layouts/application (11.1ms) Completed 500 Internal Server Error in 27ms (ActiveRecord: 0.9ms) ActionView::Template::Error (undefined method `nickname' for nil:NilClass): 26: .texts 27: .texts__header 28: .texts__header__user 29: = post.user.nickname 30: .texts__header__box 31: = link_to "Memori", post_path(post), class: 'btn btn-default' 32: = link_to "Edit", edit_post_path(post) , class: 'btn btn-default' app/views/posts/index.html.haml:29:in `block in _app_views_posts_index_html_haml__636240355849085160_70171564458420' app/views/posts/index.html.haml:25:in `_app_views_posts_index_html_haml__636240355849085160_70171564458420' Started POST "/__web_console/repl_sessions/079f2ed8f2495abb12f3f1797ec33346/trace" for ::1 at 2020-02-24 20:16:20 +0900
補足情報(FW/ツールのバージョンなど)
ruby 2.5.1
Rails 5.2.4.1
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。