我尝试在rails中验证表单。我有一个名为Vehicle
的模型,我想确保Plate
字段是必需的。这是模型中的代码:
class Vehicle < ActiveRecord::Base
mount_uploader :image, PictureUploader
validates_presence_of :plate
end
当我尝试注册新的vehicle
时,我收到以下错误:
NoMethodError in Vehicles#create
Showing /home/damaroja/webapps/gestaller/gestaller/app/views/vehicles/_form.html.erb where line #28 raised:
undefined method `map' for nil:NilClass
系统告诉我,我没有错误方法。这怎么可能?
这是_form.html.erb
<%= form_for(@vehicle) do |f| %>
<% if @vehicle.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@vehicle.errors.count, "error") %> prohibited this vehicle from being saved:</h2>
<ul>
<% @vehicle.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :image %><br>
<%= f.file_field :image %>
</div>
<div class="field">
<%= f.label :plate %><br>
<%= f.text_field :plate %>
</div>
<div class="field">
<%= f.label :places %><br>
<%= f.number_field :places %>
</div>
<div class="field">
<%= f.label :type %><br>
<%= f.select 'make', options_for_select(@listadotipos) %>
</div>
<div class="field">
<%= f.label :mam %><br>
<%= f.text_field :mam %>
.......
这是控制器代码:
def create
@vehicle = Vehicle.new(vehicle_params)
@vehicle.save
respond_with(@vehicle)
end
def new
@vehicle = Vehicle.new
tipos= Type.all
@listadotipos=[]
tipos.each do |h|
@listadotipos.push(h.name)
end
respond_with(@vehicle)
end