Undefined `errors' method in rails 4

Hi, I'm starting to chop on rails, and I'm developing a small blog application using this ref http://guides.rubyonrails.org/getting_started.html

Here I ran into a problem.

<%= form_for :post, url: posts_path do |f| %> <% if @post.errors.any? %> // Error showing undefined errors method <div id="errorExplanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> 

I am starting this Wednesday. How to solve this problem. Need help. Thanks.

+4
source share
2 answers

In the controller

 def new @post = Post.new end 

and in your view

  <%= form_for :post, url: posts_path do |f| %> <% if @post.errors.any? %> <div id="errorExplanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> 

yes, it’s just an instance of the Post object .... now your Post case is not being created, so the error is thrown so that you also need to know that errors are not a method method of the class that it will call in a specific instance see active model errors

http://api.rubyonrails.org/classes/ActiveModel/Errors.html

+9
source

In your controller:

 def new @post = Post.new end 

In your opinion:

 <%= form_for :post, url: posts_path do |f| %> <% if @post.errors.any? %> <div id="errorExplanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2> 
+3
source

Source: https://habr.com/ru/post/1499129/


All Articles