Meltemi
Meltemi

Reputation: 38359

Rails routes for get request with query params

I need a route to accept a request reports#show with an attached :query parameter and I can't figure out how to write it. It needs to respond to this link in my view:

= link_to report_path(query: params[:query]) do

config/routes.rb

  resources :reports do
    resources :chapters
    resources :pages
  end

Tried variations of: get '/reports/:id/:query', :as => 'reports_query' but I keep getting:

Routing Error

No route matches {:action=>"show", :controller=>"reports", :query=>"europe"}

Project is mostly RESTful but I'll take anything that works at this point. Thanks for any help.

Upvotes: 0

Views: 6909

Answers (2)

Nícolas Iensen
Nícolas Iensen

Reputation: 4379

I went through the same problem here, and I solved it using the default param while defining my routes.

get :twitter_form, defaults: { form: "twitter" }, as: :twitter_form, to: "campaigns#show"

Upvotes: 1

Nick Kugaevsky
Nick Kugaevsky

Reputation: 2945

You should define your route to query with code like this

# routes.rb
resources :reports do
  get ':query', to: 'reports#show', on: :member, as: :query
end

It will generate path helper you can use that way

= link_to 'Query Report', query_report_path(@report, query)

Upvotes: 1

Related Questions