我正在我的网络应用中制作一个表单,必须填写,否则我想在页面上显示错误消息。我遵循了一些教程,但我遇到了一个问题:当我提交此表单时,应用程序返回上一页而不显示错误消息,因为表单为空。
这是我的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 %>
我的控制器方法:
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
和'_add_task_error_messages.html.erb'文件,错误消息:
<% 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 %>
和模型:
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
更新:
事实上,当提交表单时,当应用程序在“新”页面上返回时,我收到错误,而不显示错误消息。 当它进入这个“新”页面时,我在html.erb上收到错误,因为我有一个必须初始化的对象。这是“新”页面:
<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>
但此错误仅在应用程序在提交的表单后重定向时显示,并且我不希望它显示“新”页面,我只想留在我的表单上,并显示空表单的错误消息
答案 0 :(得分:2)
您的文件必须是:
_add_task_error_messages.html.erb
不
add_task_error_messages.html.erb
文件名必须首先加下划线,因为它是部分的。并确保它位于app/views/shared/_add_task_error_messages.htm.erb
路径上。
更新:
尝试在渲染中传递对象。
更改:
<%= render 'shared/add_task_error_messages' %>
要:
<%= render 'shared/add_task_error_messages', task: @task %>
在_add_task_error_message.html.erb
中必须是:
<% 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 %>