Carmen-rails - 轨道问题4

时间:2013-12-13 18:49:09

标签: ruby-on-rails carmen

我一直在关注这个以设置我的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

感谢帮助。

3 个答案:

答案 0 :(得分:1)

您错过了subregion_options的路线,在您的routes.rb中,您必须添加类似

的内容
resources :jobs do
  collection do
    get :subregion_options
  end
end

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

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

现在它点击show操作并尝试查找id = subregion_options的作业,我很确定这不是您想要的:)

答案 1 :(得分: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 %>

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

答案 2 :(得分:0)

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