1

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.

Shaun Frost Duke Jackson
  • 1,256
  • 1
  • 10
  • 22

3 Answers3

1

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 :)

nathanvda
  • 49,707
  • 13
  • 117
  • 139
  • I had that last one in my routes, the top bit just gives me wrong number of arguments (1 for 0) – Shaun Frost Duke Jackson Dec 14 '13 at 01:27
  • Yes, I write this off the top of my head. Fixed it now. Note that in the routes the order is important, so alternatively it might be useful to put the `get '/jobs/subregion_options` before the `resources :jobs`. But anyway, my first suggestion is the cleanest imho and should definitely work. – nathanvda Dec 14 '13 at 10:06
  • Jesus 3 hours staring at that. Moved the routes above resources. I worked thanks – Shaun Frost Duke Jackson Dec 14 '13 at 12:16
0

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.

Donovan
  • 15,917
  • 4
  • 22
  • 34
  • What about all states depending on country? That's what I'm trying to achieve. – Shaun Frost Duke Jackson Dec 13 '13 at 19:26
  • This helper gives you all states within country code 'US', you could modify this solution to provide whatever country code you needed to use. Unfortunately, I'm not an expert in the use of carmen, It just took me a bit of exploring to get this to work for my use-case. – Donovan Dec 13 '13 at 19:29
  • I'm a little confused because on the demo app you can select a country and then the relevant states, so I might need to adjust that to 'all' maybe... – Shaun Frost Duke Jackson Dec 13 '13 at 19:30
  • The github link you posted for carmen seems to have an example using rails routes to provide the country code (via params), you could then use that to send in a country code to the helper method. Just an idea. – Donovan Dec 13 '13 at 19:33
  • Could you explain like you would to a man who has had too much wine. – Shaun Frost Duke Jackson Dec 13 '13 at 20:36
  • God I'm going to bed, this isn't making any sense. Might accept it tomorrow, this will only scope US states and not all of them depending on the country_code selected. – Shaun Frost Duke Jackson Dec 13 '13 at 20:51
  • I give up on this I cannot figure out why it is passing the id for Job. :( – Shaun Frost Duke Jackson Dec 13 '13 at 22:16
0

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