<% if @post.errors.any? %>
<% end %>
Last active
March 2, 2017 15:49
-
-
Save jendiamond/108d1e22b991ed6570cd0f116f7cabd6 to your computer and use it in GitHub Desktop.
- Given that I am at http://localhost:3000/posts/new
- When the server is running
- Then I get the error "NoMethodError in Posts#new"
I worked on it the next day. I checked the code. Suddenly it was working. It may have been a white spave issue.
undefined method errors for nil:NilClass
Extracted source (around line #5):
3
4 <%= form_for :post, url: posts_path do |f| %>
5 <% if @post.errors.any? %>
6 <div id="errors">
7 <h2><%= pluralize(@post.errors.count, "error") %> stopped this post from saving</h2>
8 <ul>- I know that this means that the Post is not being seen, so the methods
errors.any?cannot be called on it. - I'm not sure why @post is not there.
2.3.0 :018 > @post = Post.new
=> #<Post id: nil, title: nil, body: nil, created_at: nil, updated_at: nil>
2.3.0 :019 > @post.title="hi"
=> "hi"
2.3.0 :020 > @post.save
(1.4ms) BEGIN
(1.1ms) ROLLBACK
=> false
2.3.0 :021 > @post.errors
=> #<ActiveModel::Errors:0x00000002f12dc0 @base=#<Post id: nil, title: "hi", body: nil, created_at: nil, updated_at: nil>, @messages={:title=>["is too short (minimum is 5 characters)"], :body=>["can't be blank"]}, @details={:title=>[{:error=>:too_short, :count=>5}], :body=>[{:error=>:blank}]}>
2.3.0 :022 > @post.errors.any?
=> true
2.3.0 :023 > @post.errors.count
=> 2
2.3.0 :024 > @post.errors.full_messages
=> ["Title is too short (minimum is 5 characters)", "Body can't be blank"] ===
app/controllers/post_controllers.rb
class PostsController < ApplicationController
def index
@posts = Post.all.order("created_at DESC")
end
def new
@post = Post.new
end
def show
@post = Post.find(params[:id])
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end===
app/view/posts/new.html.erb
<div id="page_wrapper">
<h1 class="title">Add something</h1>
<%= form_for :post, url: posts_path do |f| %>
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "error") %> stopped this post from saving</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :title %><br/>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %><br/>
</p>
<% end %>
</div>===
app/models/post.rb
class Post < ApplicationRecord
validates :title, presence: true, length: { minimum: 5 }
validates :body, presence: true
endWithout this block of code http://localhost:3000/posts/new works fine
app/view/posts/new.html.erb
<% if @post.errors.any? %>
<div id="errors">
<h2><%= pluralize(@post.errors.count, "error") %> stopped this post from saving</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment