我的表格是:
<%= form_for @setup_limo, :html => { :class => 'form-horizontal' } do |f| %>
<%= render 'Partials/errors', object: @setup_limo %>
<div class="control-group">
<%= f.label :car_type_id, :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :car_type_id,CarType.all,:id,:name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :car_id, :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :car_id,Car.all,:id,:name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :driver_id, :class => 'control-label' %>
<div class="controls">
<%= f.collection_select :driver_id,Driver.all,:id,:name, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
setup_limos_path, :class => 'btn' %>
</div>
<% end %>
我的模特是:
class SetupLimo < ActiveRecord::Base
belongs_to :user
belongs_to :car_type
belongs_to :car
belongs_to :driver
validates :user,:car_type,:car, :driver, :presence => :true
validates_uniqueness_of :driver, :scope =>[:user, :car_type, :car]
end
我的控制器允许参数:
def setup_limo_params
params.require(:setup_limo).permit(:car_type_id, :car_id, :driver_id, :user_id)
end
创建行动:
def create
@setup_limo = SetupLimo.new(setup_limo_params)
respond_to do |format|
if @setup_limo.save
format.html { redirect_to @setup_limo, notice: 'Setup limo was successfully created.' }
format.json { render action: 'show', status: :created, location: @setup_limo }
else
format.html { render action: 'new' }
format.json { render json: @setup_limo.errors, status: :unprocessable_entity }
end
end
end
但是在创建时我得到了错误:日志是:
Started POST "/setup_limos" for 127.0.0.1 at 2013-08-09 21:02:05 +0530
Processing by SetupLimosController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"######=", "setup_limo"=>{"car_type_id"=>"3", "driver_id"=>""}, "commit"=>"Create Setup limo"}
[1m[35mCACHE (0.0ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 [["id", 1]]
[1m[36mCarType Load (0.4ms)[0m [1mSELECT `car_types`.* FROM `car_types` WHERE `car_types`.`id` = 3 ORDER BY `car_types`.`id` ASC LIMIT 1[0m
[1m[35m (0.3ms)[0m ROLLBACK
Completed 500 Internal Server Error in 482ms
NoMethodError (undefined method `attributes' for nil:NilClass):
app/controllers/setup_limos_controller.rb:35:in `block in create'
app/controllers/setup_limos_controller.rb:34:in `create'
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.1ms)
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms)
Rendered /Users/MohammedSha/.rvm/gems/ruby-2.0.0-p195/gems/actionpack-4.0.0.rc2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (42.6ms)
我使用的是Rails 4 提交时我得到了上述错误, 我想验证用户,car_type和car的驱动程序的唯一性 谢谢你的帮助..
答案 0 :(得分:0)
也许我在描述中遗漏了一些东西,但你对驱动程序进行了presence
验证,并且在传递的参数中它是空的。
如果驱动程序可以为空,则将其从SetupLimo
上的验证中删除:
validates :user, :car_type, :car, :presence => :true