Karz
Karz

Reputation: 557

Ruby On Rails form error message

I'm making a form in my web app, that has to be filled, else I want to display an error message on the page. I followed some tutorials, but I'm encountering an issue : when I submit this form, the app is returning on the previous page without displaying the error message because the form is empty.

Here is my html.erb :

<%= form_for @task do |f| %>
  <%= render 'shared/add_task_error_messages' %>
  <%= f.text_field :title, :class => "col-lg-4 col-lg-offset-4 field", :placeholder => "Title", :maxlength => "80" %></br>
  <%= f.text_field :department, :class => "col-lg-4 col-lg-offset-4 field", :placeholder => "Department", :maxlength => "80" %></br>
  <%= f.text_field :startDate, :class => "col-lg-4 col-lg-offset-4 datepicker", :placeholder => "Start date", :maxlength => "80" %></br>
  <%= f.text_field :endDate, :class => "col-lg-4 col-lg-offset-4 datepicker", :placeholder => "End date", :maxlength => "80" %></br>
  <%= f.submit :Submit, :value => "Create", :class => "col-lg-2 col-lg-offset-5", :id => "create_task" %>
<% end %>

my controller methods :

def create
  @task = Task.new(task_params)
  if @task.save
    redirect_to @task
  else
    render 'new'
  end
end

private

def task_params
    params.require(:task).permit(:title, :department, :startDate, :endDate)
end

and the '_add_task_error_messages.html.erb' file with the error message :

<% if @task.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(@task.errors.count, "error") %>.
    </div>
    <ul>
    <% @task.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

and the model :

class Task < ActiveRecord::Base

  validates :title, :presence => true
  validates :department, :presence => true
  validates :startDate, :presence => true
  validates :endDate, :presence => true

  # Returns tasks array of the project
  def self.getProjectTasks
    projectTasks = Array.new
    result = Task.where("identifier = ?", 1)
    (1..result.length).each do |i|
      task = Array.new
      task.push(result.find(i).title)
      task.push(result.find(i).department)
      task.push(result.find(i).content)
      task.push(result.find(i).duration)
      task.push(result.find(i).startDate)
      task.push(result.find(i).endDate)
      projectTasks.push(task)
    end
    return projectTasks
  end

end

UPDATE :

In fact, I'm getting an error when the app is returning on the 'new' page when the form is submitted, without showing the error message. When it go on this 'new' page, I'm getting an error on the html.erb because I have an object which has to be initialized. Here is the 'new' page :

<div class="tasks">
  <% @projectTasks.each do |task| %> # The error is here, "undefined method `each' for nil:NilClass"
    <div class="task col-lg-8">
      <div class="details">
        <div class="detail"><%= task[0] %></div>
        <div class="detail"><%= task[1] %></div>
        <div class="detail"><%= task[3] %> days</div>
        <div class="detail"><%= task[4] %> - <%= task[5] %></div>
      </div>
    </div>
    <% end %>
  </div>

but this error is only showing when the app is redirecting after the submitted form, and I don't want it to display the 'new' page, I only want to stay on my form with the error message of the empty form.

Upvotes: 0

Views: 1945

Answers (1)

HashRocket
HashRocket

Reputation: 798

Your file must be:

_add_task_error_messages.html.erb

Not:

add_task_error_messages.html.erb

Filename must have underscore first because it is a partial. And be sure that it is on app/views/shared/_add_task_error_messages.htm.erb path.

UPDATE:

Try to pass the object within you rendering.

Change:

<%= render 'shared/add_task_error_messages' %>

To:

<%= render 'shared/add_task_error_messages', task: @task %>

And in _add_task_error_message.html.erb it must be:

<% if task.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-danger">
      The form contains <%= pluralize(task.errors.count, "error") %>.
    </div>
    <ul>
    <% task.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
<% end %>

Upvotes: 2

Related Questions