Carmen-rails - Issue with rails 4

431 Views Asked by At

I have been following this to set up state/country drop downs for my rails application but notice that I'm getting the following error:

Started GET "/jobs/subregion_options?parent_region=BR" for 127.0.0.1 at 2013-12-13 21:01:09 +0000
Processing by JobsController#show as HTML
  Parameters: {"parent_region"=>"BR", "id"=>"subregion_options"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  Job Load (0.2ms)  SELECT "jobs".* FROM "jobs" WHERE "jobs"."id" = ? LIMIT 1  [["id", "subregion_options"]]
Completed 404 Not Found in 4ms

ActiveRecord::RecordNotFound (Couldn't find Job with id=subregion_options):
  app/controllers/jobs_controller.rb:75:in `set_job'

I cannot understand why this is doing this when my set_job filter is only as shown below:

before_action :set_job, only: [:show, :edit, :update, :destroy] 

Here is the link I'm following with he use of a partial and routes:

https://github.com/jim/carmen-demo-app

Routes

                    jobs GET      /jobs(.:format)                        jobs#index
                         POST     /jobs(.:format)                        jobs#create
                 new_job GET      /jobs/new(.:format)                    jobs#new
                edit_job GET      /jobs/:id/edit(.:format)               jobs#edit
                     job GET      /jobs/:id(.:format)                    jobs#show
                         PATCH    /jobs/:id(.:format)                    jobs#update
                         PUT      /jobs/:id(.:format)                    jobs#update
                         DELETE   /jobs/:id(.:format)                    jobs#destroy
                    root GET      /                                      pages#index
  jobs_subregion_options GET      /jobs/subregion_options(.:format)      jobs#subregion_options

Appreciate the help.

3

There are 3 best solutions below

3
On BEST ANSWER

You are missing the route for subregion_options, in your routes.rb you will have to add something like

resources :jobs do
  collection do
    get :subregion_options
  end
end

Or, as suggested in the readme of the demo-app:

get '/jobs/subregion_options' => 'jobs#subregion_options'

Now it hits the show action and tries to look for a job with id = subregion_options, which I am pretty sure is not what you want :)

0
On

It is passing the id for Job because it is the first match of the route. Could you show me your routes.rb?

7
On

I had this same problem, I just needed the US States in a select.

Here's the code I used which resolved it (for me)

module ApplicationHelper
  def us_states
    Carmen::Country.coded('US').subregions.map { |c| c.code }
  end
end

...and then in my view:

<%= f.input_field :state, collection: us_states, include_blank: false %>

Example using PARAMS to provide Country Code

module ApplicationHelper
  def get_subregions(country_code = 'US')
    Carmen::Country.coded(country_code).subregions.map { |c| c.code }
  end
end

Then, generate a route to your view, capturing a param, like www.mysite.com/myform/US

get 'myform/:cc', to: 'mycontroller#edit'

and finally, in your view use this as input to your helper. Like thus:

<%= f.input_field :state, collection: get_subregions(params[:cc]), include_blank: false %>

Note: this is purely pseudocode, so you may have to tweak it to get it to work properly.