简体   繁体   中英

Trying to show error messages, but running into: undefined method `errors' for nil:NilClass

I am trying to implement error messages in Rails and I somehow fail to get it to work at one point.

The View I am trying to add the error messages:

<%= form_for([@post, @post.comments.build], html: {class: 'form-horizontal'}) do |f| %>

            <% if @comment.errors.any? %>

                <div id="errorExplanation" class="span5 offset1 alert alert-error">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>

                    <h4>Something went wrong!</h4><br>

                    <% @post.errors.full_messages.each do |msg| %>
                    <p><%= msg %></p>
                    <% end %>
                </div>

        <% end %>

And the controller:

def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end

And the error message:

undefined method `errors' for nil:NilClass Extracted source (around line #65):

<li>
            <%= form_for([@post, @post.comments.build], html: {class: 'form-horizontal'}) do |f| %>

65 ->                       <% if @comment.errors.any? %>

                    <div id="errorExplanation" class="span5 offset1 alert alert-error">
                        <button type="button" class="close" data-dismiss="alert">&times;</button>

The Comments of course belong to a post which can have many comments. Any help? I tried everything I could think of (which is not much since I am new to RoR ;) - including various ways of trying to get the error messages (@post.comment.errors.any? etc.).

Thanks in advance. Timo

From your comments there are many things going on here.

You're trying to create a comment, so the form action should be CommentsController#create. The view makes sense that it would be PostsController#show (you don't specify), and before rendering you need to instantiate @comment. Possibly:

PostsController

def show
  @post = Post.find(params[:id])
  @comment = @post.comments.build
end

CommentsController

def create
  @post = Post.find(params[:post_id])
  @comment = @post.comments.build(params[:comment].permit(:commenter, :body))
  if @comment.save
    redirect_to post_path(@post)
  else
    render :file => "posts/show"
  end 
end

Note that you must render and not redirect so you keep a hold of the @comment instance and can render errors.

posts/show.html.erb

<%= form_for([@post, @post.comments.build], html: {class: 'form-horizontal'}) do |f| %>
    <% if @comment.errors.any? %>

Whether this is correct will depend on your routes.rb. I'm assuming comments is a nested resource in posts, which is what your question leads to think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM