Rails 使用查询字符串获取资源的路由
Rails route to get a resource using query string
我想要一个基于查询字符串的自定义路由,以访问指定的资源。例如:
/opportunities/rent/san-miguel-de-tucuman?id=45045
该路由应映射到操作 OpportunitiesController#show,资源 ID 为 45045。
提前致谢!!!
已更新
这是我当前的路线:
- 得到'oportunidades/alquiler/san-miguel-de-tucuman/:id',到:"opportunities#show"
- 得到'oportunidades/alquiler/san-miguel-de-tucuman',到:"opportunities#index"
所以,如果我导航到 /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 路线,它会转到 Opportunities#index 操作。
P/S: 对不起,我忘了说我有一个类似的索引操作路线。
使用这个
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
并将一个对象传递给如此创建的路径。例如:-
something_path(@object),这里的@object 是带有id 的对象,会在routes
将您的自定义路线设为:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
并将您的 'id' 作为 opportunities_show_path(id)
编辑
将您的路线更改为:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
现在,当您想要访问您的展示页面时,只需使用 opportunities_show_path(:id =>123456 )
索引页使用 opportunities_index_path
选项 1
查询字符串参数
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
选项 2
像新参数一样添加id。更友好。
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
在这两种情况下,您都需要这样调用路由:
show_opportunity_url(:id => 45045)
在您的控制器中,您将获得 params[:id]
中的 ID
我想要一个基于查询字符串的自定义路由,以访问指定的资源。例如:
/opportunities/rent/san-miguel-de-tucuman?id=45045
该路由应映射到操作 OpportunitiesController#show,资源 ID 为 45045。
提前致谢!!!
已更新
这是我当前的路线:
- 得到'oportunidades/alquiler/san-miguel-de-tucuman/:id',到:"opportunities#show"
- 得到'oportunidades/alquiler/san-miguel-de-tucuman',到:"opportunities#index"
所以,如果我导航到 /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 路线,它会转到 Opportunities#index 操作。
P/S: 对不起,我忘了说我有一个类似的索引操作路线。
使用这个
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
并将一个对象传递给如此创建的路径。例如:-
something_path(@object),这里的@object 是带有id 的对象,会在routes
将您的自定义路线设为:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
并将您的 'id' 作为 opportunities_show_path(id)
编辑
将您的路线更改为:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
现在,当您想要访问您的展示页面时,只需使用 opportunities_show_path(:id =>123456 )
索引页使用 opportunities_index_path
选项 1
查询字符串参数
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
选项 2
像新参数一样添加id。更友好。
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
在这两种情况下,您都需要这样调用路由:
show_opportunity_url(:id => 45045)
在您的控制器中,您将获得 params[:id]