Method returns me as null

Asked

Viewed 23 times

0

When I create my code below, it returns me this error message, saying that my method is undefined for nil (undefined method 'errors' for nil:NilClass)

Code:

<ul>
  <% @task.errors.full_messages.each do |message| %>
    <li><%= message %></li> 
  <%end%>
</ul>

<%=form_for @task do |f|%>
    <%=f.label :description, 'Descrição'%>
    <%=f.text_field :description%>
    <%=f.label :status, 'Status'%>
    <%=f.check_box :status%>
    <%=f.submit 'Criar'%>
<%end%>

1 answer

0

In the controller, you must have a action pointing to this view. There in the action, @task needs to be instantiated. See:

def new
  @task = Task.new
end
  • I have a Tasks_controller created. Inside the <ul> I added the following line <% if @task.is_a?(Task) && ! @task.errors.fullmessages.Empty? %> and in my form_for left as Tasks.new do and it worked !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.