Home>

Method reference error in text database implementation

NoMethodError in Posts # new
undefined method `errors'for nil: NilClass

<h2>Add New Post</h2>
<% = form_for: post, url: posts_path do | f |%>
<p>
  <% = f.text_field: title, placeholder: 'enter title'%>
  <% if @ post.errors.messages [: title] .any?%>
  <span><% = @ post.errors.messages [: title] [0]%></span>
  <% end%>
</p>
<p>
  <% = f.text_area: body, placeholder: 'enter body text'%>
  <% if @ post.errors.messages [: body] .any?%>
  <span><% = @ post.errors.messages [: body] [0]%></span>
  <% end%>
</p>
<p>
  <% = f.submit%>
</p>
<% end%>
class PostsController<ApplicationController
  def new
    @post = Post.new
  end
  def create
       @post = Post.new (post_params)
    if @ post.save
      redirect_to posts_path
    else
      render 'new'
    end
  end
   private
     def post_params
      params.require (: post) .permit (: title,: body)
     end
end

Since an empty post instance is created by the new action, I think that the error part is a different part.
Professor

  • Answer # 1

    Updating the atom body and checking the file again, the file information was restored to the previous save information.
    Redefine it normally and resolve it safely.

    Since a similar thing happened with atom before, was it debugged with an update?

Related articles