私のフォームは次のとおりです。
<%= 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 のドライバーの一意性を検証したい 助けてくれてありがとう..