質問編集履歴
3
誤字
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -54,7 +54,7 @@ 
     | 
|
| 
       54 
54 
     | 
    
         
             
              before_action :authenticate_user
         
     | 
| 
       55 
55 
     | 
    
         | 
| 
       56 
56 
     | 
    
         
             
              def index
         
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
      
 57 
     | 
    
         
            +
                  @posts = Post.all.order(created_at: :desc)
         
     | 
| 
       58 
58 
     | 
    
         
             
              end
         
     | 
| 
       59 
59 
     | 
    
         | 
| 
       60 
60 
     | 
    
         
             
              def show
         
     | 
| 
         @@ -63,7 +63,7 @@ 
     | 
|
| 
       63 
63 
     | 
    
         
             
              end
         
     | 
| 
       64 
64 
     | 
    
         | 
| 
       65 
65 
     | 
    
         
             
              def new
         
     | 
| 
       66 
     | 
    
         
            -
             
     | 
| 
      
 66 
     | 
    
         
            +
                  @post = Post.new
         
     | 
| 
       67 
67 
     | 
    
         
             
              end
         
     | 
| 
       68 
68 
     | 
    
         
             
              def create
         
     | 
| 
       69 
69 
     | 
    
         
             
              @post = Post.new(
         
     | 
| 
         @@ -97,10 +97,9 @@ 
     | 
|
| 
       97 
97 
     | 
    
         
             
              def destroy
         
     | 
| 
       98 
98 
     | 
    
         
             
                @post = Post.find_by(id: params[:id])
         
     | 
| 
       99 
99 
     | 
    
         
             
            @post.destroy
         
     | 
| 
       100 
     | 
    
         
            -
             
     | 
| 
      
 100 
     | 
    
         
            +
                  flash[:notice] = "投稿を削除しました"
         
     | 
| 
       101 
101 
     | 
    
         
             
                redirect_to("/posts/index")
         
     | 
| 
       102 
102 
     | 
    
         
             
              end
         
     | 
| 
       103 
     | 
    
         
            -
             
     | 
| 
       104 
103 
     | 
    
         
             
            ```ここに言語を入力
         
     | 
| 
       105 
104 
     | 
    
         
             
            コード
         
     | 
| 
       106 
105 
     | 
    
         | 
2
posts_controller_erb 追加
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -49,7 +49,58 @@ 
     | 
|
| 
       49 
49 
     | 
    
         
             
              </div>
         
     | 
| 
       50 
50 
     | 
    
         
             
            </div>
         
     | 
| 
       51 
51 
     | 
    
         
             
            ソースコード 
         
     | 
| 
      
 52 
     | 
    
         
            +
            ---posts_controller.erb----
         
     | 
| 
      
 53 
     | 
    
         
            +
            class PostsController < ApplicationController
         
     | 
| 
      
 54 
     | 
    
         
            +
              before_action :authenticate_user
         
     | 
| 
      
 55 
     | 
    
         
            +
              
         
     | 
| 
      
 56 
     | 
    
         
            +
              def index
         
     | 
| 
      
 57 
     | 
    
         
            +
                @posts = Post.all.order(created_at: :desc)
         
     | 
| 
      
 58 
     | 
    
         
            +
              end
         
     | 
| 
      
 59 
     | 
    
         
            +
              
         
     | 
| 
      
 60 
     | 
    
         
            +
              def show
         
     | 
| 
      
 61 
     | 
    
         
            +
                @post = Post.find_by(id: params[:id])
         
     | 
| 
      
 62 
     | 
    
         
            +
                @user = @post.user
         
     | 
| 
      
 63 
     | 
    
         
            +
              end
         
     | 
| 
      
 64 
     | 
    
         
            +
              
         
     | 
| 
      
 65 
     | 
    
         
            +
              def new
         
     | 
| 
      
 66 
     | 
    
         
            +
                @post = Post.new
         
     | 
| 
      
 67 
     | 
    
         
            +
              end
         
     | 
| 
      
 68 
     | 
    
         
            +
              def create
         
     | 
| 
      
 69 
     | 
    
         
            +
              @post = Post.new(
         
     | 
| 
      
 70 
     | 
    
         
            +
                content: params[:content],
         
     | 
| 
      
 71 
     | 
    
         
            +
                user_id: @current_user.id
         
     | 
| 
      
 72 
     | 
    
         
            +
              )
         
     | 
| 
       52 
73 
     | 
    
         | 
| 
      
 74 
     | 
    
         
            +
                if @post.save
         
     | 
| 
      
 75 
     | 
    
         
            +
                  flash[:notice] = "投稿を作成しました"
         
     | 
| 
      
 76 
     | 
    
         
            +
                  redirect_to("/posts/index")
         
     | 
| 
      
 77 
     | 
    
         
            +
                else
         
     | 
| 
      
 78 
     | 
    
         
            +
                  render("posts/new")
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
              end
         
     | 
| 
      
 81 
     | 
    
         
            +
              
         
     | 
| 
      
 82 
     | 
    
         
            +
              def edit
         
     | 
| 
      
 83 
     | 
    
         
            +
                @post = Post.find_by(id: params[:id])
         
     | 
| 
      
 84 
     | 
    
         
            +
              end
         
     | 
| 
      
 85 
     | 
    
         
            +
              
         
     | 
| 
      
 86 
     | 
    
         
            +
              def update
         
     | 
| 
      
 87 
     | 
    
         
            +
                @post = Post.find_by(id: params[:id])
         
     | 
| 
      
 88 
     | 
    
         
            +
                @post.content = params[:content]
         
     | 
| 
      
 89 
     | 
    
         
            +
                if @post.save
         
     | 
| 
      
 90 
     | 
    
         
            +
                  flash[:notice] = "投稿を編集しました"
         
     | 
| 
      
 91 
     | 
    
         
            +
                  redirect_to("/posts/index")
         
     | 
| 
      
 92 
     | 
    
         
            +
                else
         
     | 
| 
      
 93 
     | 
    
         
            +
                  render("posts/edit")
         
     | 
| 
      
 94 
     | 
    
         
            +
                end
         
     | 
| 
      
 95 
     | 
    
         
            +
              end
         
     | 
| 
      
 96 
     | 
    
         
            +
              
         
     | 
| 
      
 97 
     | 
    
         
            +
              def destroy
         
     | 
| 
      
 98 
     | 
    
         
            +
                @post = Post.find_by(id: params[:id])
         
     | 
| 
      
 99 
     | 
    
         
            +
            @post.destroy
         
     | 
| 
      
 100 
     | 
    
         
            +
                flash[:notice] = "投稿を削除しました"
         
     | 
| 
      
 101 
     | 
    
         
            +
                redirect_to("/posts/index")
         
     | 
| 
      
 102 
     | 
    
         
            +
              end
         
     | 
| 
      
 103 
     | 
    
         
            +
             
     | 
| 
       53 
104 
     | 
    
         
             
            ```ここに言語を入力
         
     | 
| 
       54 
105 
     | 
    
         
             
            コード
         
     | 
| 
       55 
106 
     | 
    
         | 
1
誤字
    
        title	
    CHANGED
    
    | 
         
            File without changes
         
     | 
    
        body	
    CHANGED
    
    | 
         @@ -3,7 +3,7 @@ 
     | 
|
| 
       3 
3 
     | 
    
         
             
            progateのRuby on Rails5 学習コース Ⅸ 「 新規投稿をログインユーザーに紐付けよう」 のところで、投稿一覧にユーザー情報を表示ができず```index.html.erb
         
     | 
| 
       4 
4 
     | 
    
         
             
            で```undefined method `image_name' for nil:NilClassというエラーが発生しています。
         
     | 
| 
       5 
5 
     | 
    
         | 
| 
       6 
     | 
    
         
            -
            postは存在しているのですが、それに紐づいているはずの 
     | 
| 
      
 6 
     | 
    
         
            +
            postのuser_idは存在しているのですが、それに紐づいているはずのuser_idがnilとなっており、postに対しuser_idがうまく紐づいてくれません。また、postでuser_idを保存しようとしても保存できず、falseが返ってきます。
         
     | 
| 
       7 
7 
     | 
    
         
             
            以下のpost.rbとuser.erbのコードでは関連づけられていないのでしょうか?
         
     | 
| 
       8 
8 
     | 
    
         | 
| 
       9 
9 
     | 
    
         
             
            ### 発生している問題・エラーメッセージ```NoMethodError in Posts#indexundefined method `image_name' for nil:NilClass
         
     |