Home>

The title error has occurred.
Ruby has almost never been touched, and I'm sorry for the simple question.
Since @post is defined, it is recognized that there is no method and it is not undefined.
Is this error because @post is nil?

If it is

, why is it nil?
I would appreciate any advice.

Error

NoMethodError in Posts # edit
Showing /home/progate/tweet_app/app/views/posts/edit.html.erb where line # 6 raised:
undefined method `content 'for nil: NilClass
Extracted source (around line # 6):

    <div>
      <div>
       <textarea><% = @ post.content%></textarea>
        <input type = "submit" value = "Save">
      </div>
    </div>

Rails.root:/home/progate/tweet_app
Application Trace | Framework Trace | Full Trace
app/views/posts/edit.html.erb: 6: in `_app_views_posts_edit_html_erb__4353119297950482048_47327653967320 '
Request
Parameters:
{"instanceId" =>"2d16f8d22d4a", "containerPort" =>"3000", "languageName" =>"rails5", "locale" =>"ja", "id" =>"4" }
Toggle session dump
Toggle env dump
Response
Headers:
None

edit.html.erb

<div>
  <div>
    <h1>Edit</h1>
    <div>
      <div>
       <textarea><% = @ post.content%></textarea>
        <input type = "submit" value = "Save">
      </div>
    </div>
  </div>
</div>


posts.controller.rb

class PostsController<ApplicationController
  def index
    @posts = Post.all.order (created_at:: desc)
  end
  def show
    @post = Post.find_by (id: params [: id])
  end
  def new
    @post = Post.new
  end
  def create
    @post = Post.new (content: params [: content])
    if @ post.save
      flash [: notice] = "Post created"
      redirect_to ("/ posts/index")
    else
      render ("posts/new")
    end
    def edit
      @post = Post.find_by (id: params [: id])
    end

  end
end

routes.rb

Rails.application.routes.draw do
  get "posts/index" =>"posts # index"
  get "posts/new" =>"posts # new"
  get "posts /: id" =>"posts # show"
  post "posts/create" =>"posts # create"
  get "posts /: id/edit" =>"posts # edit"

  get "/" =>"home # top"
  get "about" =>"home # about"
end

show.html.erb

<div>
  <div>
    <div>
      <p>
        <% = @ post.content%>
      </p>
      <div>
        <% = @ post.created_at%>
        <div>
        <% = link_to ("edit", "/posts/#{@post.id}/edit")%>
      </div>
      </div>
    </div>
  </div>
</div>

The posts database has columns id, content, created_at, and update_at.

  • Answer # 1

    The definition of the edit method is in the definition of the create method.

    def create
        @post = Post.new (content: params [: content])
        if @ post.save
          flash [: notice] = "Post created"
          redirect_to ("/ posts/index")
        else
          render ("posts/new")
        end
        def edit
          @post = Post.find_by (id: params [: id])
        end
      end

    instead of

    def create
        @post = Post.new (content: params [: content])
        if @ post.save
          flash [: notice] = "Post created"
          redirect_to ("/ posts/index")
        else
          render ("posts/new")
        end
      end
      def edit
        @post = Post.find_by (id: params [: id])
      end
    Let's make it

    .

  • Answer # 2

      

    Since @post is defined, there is no method, but it is recognized that it is not undefined.

    Debugging is not what you think.
    What is actually going on?
    Write code to check whether it is undefined at that point in the code.

  • Answer # 3

    edit.html uses print statements to see what happens to the value of @posts, Please check by using.

    def edit ... part of indent is not aligned with other def indents. Is it okay?