1

我一直在关注此为我的 rails 应用程序设置州/国家/地区下拉菜单,但请注意我收到以下错误:

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'

当我的 set_job 过滤器仅如下所示时,我无法理解为什么会这样做:

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

这是我使用部分和路线所遵循的链接:

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

路线

                    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

感谢帮助。

4

3 回答 3

1

你错过了路线subregion_optionsroutes.rb你将不得不添加类似的东西

resources :jobs do
  collection do
    get :subregion_options
  end
end

或者,正如演示应用程序的自述文件中所建议的那样:

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

现在它开始show行动并尝试用 id = 寻找工作subregion_options,我很确定这不是你想要的:)

于 2013-12-13T23:25:52.047 回答
0

我有同样的问题,我只需要选择美国。

这是我用来解决它的代码(对我来说)

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

...然后在我看来:

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

使用 PARAMS 提供国家代码的示例

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

然后,生成一条到您的视图的路线,捕获一个参数,例如www.mysite.com/myform/US

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

最后,在您看来,将其用作助手的输入。像这样:

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

注意:这纯粹是伪代码,因此您可能需要对其进行调整才能使其正常工作。

于 2013-12-13T19:23:16.383 回答
0

它正在传递 Job 的 id,因为它是路线的第一个匹配项。你能告诉我你的routes.rb吗?

于 2013-12-13T23:11:27.587 回答