簡體   English   中英

帶有查詢參數的獲取請求的Rails路由

[英]Rails routes for get request with query params

我需要一個路由接受請求reports#show with an :query參數,我無法弄清楚如何編寫它。 它需要在我的視圖中回復此鏈接:

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

配置/ routes.rb中

  resources :reports do
    resources :chapters
    resources :pages
  end

嘗試過以下變體: get '/reports/:id/:query', :as => 'reports_query'但我一直得到:

Routing Error

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

項目主要是RESTful,但我會采取任何有效的方法。 謝謝你的幫助。

您應該使用這樣的代碼定義要查詢的路由

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

它將生成您可以使用這種方式的路徑助手

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

我在這里遇到了同樣的問題,我在定義路線時使用default參數解決了它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM