当我尝试使用参数创建路由时,获取“缺失:路由定义上的操作键”

时间:2018-01-29 15:45:27

标签: ruby-on-rails parameters routes ruby-on-rails-5

我有一个带有Rails 5的应用程序。我想在我的模型中创建一个搜索名称的路径,看起来像

/people/search/search_str

所以我在config / routes.rb文件中创建了以下路由

  resources :people do
    collection do
      get 'image/:id', :to => "people#image", :as => 'image'
      get 'ranks', :to => "people#ranks", :as => 'ranks'
      get 'search/:search(.:format)', to: :search
    end
  end

但这导致以下错误

/Users/nataliab/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/routing/mapper.rb:329:in `check_part': Missing :action key on routes definition, please check your routes. (ArgumentError)
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/routing/mapper.rb:318:in `check_controller_and_action'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/routing/mapper.rb:252:in `normalize_options!'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/routing/mapper.rb:116:in `initialize'
    from /Users/nataliab/.rvm/gems/ruby-2.4.0/gems/actionpack-5.0.6/lib/action_dispatch/routing/mapper.rb:69:in `new'

构建路线的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

get 'search/:search(.:format)', to: 'people#search'