一切正常,直到我想添加第二个collection_select,以便在我的开场/新视图中从当前用户中选择属于公司的位置。我阅读并尝试了不同的方法,但仍然没有工作。
Jobdescription collection_select很好,但我不知道问题是否来自,即使我怀疑控制器。
如果你能告诉我问题出在哪里,那将是非常好的。感谢。
我的schema.rb
create_table "companies", force: true do |t|
t.string "company_name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "openings", force: true do |t|
t.integer "jobdescription_id"
t.integer "location_id"
t.date "ending"
t.datetime "created_at"
t.datetime "updated_at"
create_table "locations", force: true do |t|
t.string "name"
t.string "postalcode"
t.float "lat"
t.float "lon"
t.integer "company_id"
t.datetime "created_at"
t.datetime "updated_at"
end

Company.rb
class Company < ActiveRecord::Base
has_many :users
has_many :jobdescriptions
has_many :locations
end
&#13;
Location.rb
class Location < ActiveRecord::Base
belongs_to :company
has_many :openings
geocoded_by :postalcode,
:latitude => :lat, :longitude => :lon
after_validation :geocode
end
&#13;
Opening.rb
class Opening < ActiveRecord::Base
belongs_to :company
belongs_to :jobdescription
belongs_to :location
end
&#13;
应用程序/控制器/ openings_controller.rb
class OpeningsController < ApplicationController
layout 'application'
def new
@opening = Opening.new
@jobdescriptions = current_user.company.jobdescriptions
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @jobdescription }
end
@locations = current_user.company.locations
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @location }
end
end
&#13;
应用程序/视图/开口/ new.html.erb
<div class='form-group'>
<label>Job desc</label>
<br/>
<%= collection_select(:opening, :jobdescription_id, @jobdescriptions, :id, :job_title, {:prompt => false}) %>
</div>
<div class='form-group'>
<label>location</label>
<br/>
<%= collection_select(:opening, :location_id, @locations, :id, :name, {:prompt => false}) %>
</div>
&#13;
答案 0 :(得分:-1)
<%= collection_select(:opening, :location_id, current_user.company.locations, :id, :name, {:prompt => false}) %>