0

Everything was working until I wanted to add a second collection_select in order to select the location that belongs to the company from the current user in my openings/new view. I read and tried different ways but still not working.

Jobdescription collection_select works well alone, but I do not know if the problem is from, even if I suspect the controller.

if you can tell me where the issue is, that would be very kind of you. Thanks.

My 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

Location.rb

class Location < ActiveRecord::Base
  belongs_to :company
  has_many :openings

  geocoded_by :postalcode,
              :latitude => :lat, :longitude => :lon
  after_validation :geocode
end

Opening.rb

class Opening < ActiveRecord::Base
  belongs_to :company
  belongs_to :jobdescription
  belongs_to :location
end

app/controllers/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

app/views/openings/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>
reme
  • 183
  • 1
  • 1
  • 12
  • 1
    you posted every thing except the actual view that has the error – Mohammad AbuShady Feb 21 '15 at 20:04
  • Yes sorry I forgot, thanks – reme Feb 21 '15 at 20:05
  • @reme - You are using `@location` variable in your view, but you have not declared it in your controller. – BroiSatse Feb 21 '15 at 20:12
  • @BroiSatse, in my view i'm using '@locations' which is declared in my controller. im sorry i do not see '@location' in my view or im blind. thanks – reme Feb 21 '15 at 20:20
  • Time to visit the optometrist. <%= collection_select(:opening, :location_id, @locations, :id, :name, {:prompt => false}) %> – MarsAtomic Feb 21 '15 at 20:31
  • @MarsAtomic, I do not see '@location' in my collection_select. – reme Feb 21 '15 at 20:56
  • Again, with emphasis: <%= collection_select(:opening, :location_id, **@locations**, :id, :name, {:prompt => false}) %> – MarsAtomic Feb 21 '15 at 21:01
  • OK, firstly remove the second `respond_to` from your action and then move @locations declaration above the first one. Sorry, I meant `@locations` in my first comment. – BroiSatse Feb 21 '15 at 21:05
  • @BroiSatse, thanks your your reply, it works now, you can repost it as an answer, and then I will accept it. regards,. – reme Feb 21 '15 at 22:10

1 Answers1

-1
<%= collection_select(:opening, :location_id, current_user.company.locations, :id, :name, {:prompt => false}) %>
Josh
  • 5,631
  • 1
  • 28
  • 54
reme
  • 183
  • 1
  • 1
  • 12
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – dgilperez Feb 22 '15 at 02:35
  • 1 Yes it an answer for me as it has fixed my issue, I answered my own question because they gave the right to do so, if you are happy with that then contact the site owner. – reme Feb 22 '15 at 03:48