【问题标题】:Rails route to get a resource using query stringRails路由使用查询字符串获取资源
【发布时间】:2015-05-05 11:13:57
【问题描述】:

我想有一个基于自定义路由查询字符串,以访问指定的资源。例如:

/opportunities/rent/san-miguel-de-tucuman?id=45045

该路由应映射到操作 OpportunitiesController#show,资源 ID 为 45045。

提前致谢!!!

更新

这是我目前的路线:

  • get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' 到:“opportunities#show”
  • get 'oportunidades/alquiler/san-miguel-de-tucuman',到:“opportunities#index”

因此,如果我导航到 /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 路线,它会转到 Opportunities#index 操作。

P/S:抱歉,我忘了提到我有类似的 index 操作路线。

【问题讨论】:

  • 我们能看到rake routes 的输出吗?另外,您的 routes.rb 文件看起来如何。

标签: ruby-on-rails routing url-routing


【解决方案1】:

将您的自定义路线设为:

resources: opportunities, except: :show

get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'

并将您的“id”作为机会_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

【讨论】:

  • 这是我目前的路线:get 'oportunidades/alquiler/san-miguel-de-tucuman/:id',到:"opportunities#show" get 'oportunidades/alquiler/san-miguel-de -tucuman', to: "opportunities#index" 因此,如果我导航到 /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 路线,它会转到 Opportunities#index 操作。 P/S:对不起,我忘了提到我有一个类似的 index 动作路线。
  • 如果要访问索引操作,那么您的路径必须类似于 /oportunidades/alquiler/san-miguel-de-tucuman,如果您要访问显示操作,那么路径必须是 /oportunidades/alquiler/san -miguel-de-tucuman/123456
  • 那不起作用,因为如果我使用路由到收集操作的 url /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 => Opportunities#index 那应该路由到操作 Opportunities#show 操作。
  • 但是你为什么要像查询字符串 (?id=1234567) 一样传递 id 来进行显示操作,只需将其用作 /oportunidades/alquiler/san-miguel-de-tucuman/123456
【解决方案2】:

使用这个

match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get

并将对象传递给如此创建的路径。例如:-

something_path(@object),这里@object 是带有id 的对象,将在路由中传递

【讨论】:

  • 这是我目前的路线:get 'oportunidades/alquiler/san-miguel-de-tucuman/:id',到:"opportunities#show" get 'oportunidades/alquiler/san-miguel-de -tucuman', to: "opportunities#index" 因此,如果我导航到 /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 路线,它会转到 Opportunities#index 操作。 P/S:对不起,我忘了提到我有类似的 index 操作路线。
  • @AugustoPedraza ID 应该只传递给 show、edit 和 destroy 方法作为最佳路由实践
  • 你是对的。但是,在 RESTfull 上,当您在集合路由中使用查询字符串时,您将过滤该集合。但是,考虑到它对 Rails 来说是模糊的,我将遵循标准的 rails 路线约定。恕我直言,谢谢!
【解决方案3】:

选项 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

【讨论】:

  • 这是我目前的路线:get 'oportunidades/alquiler/san-miguel-de-tucuman/:id',到:"opportunities#show" get 'oportunidades/alquiler/san-miguel-de -tucuman', to: "opportunities#index" 因此,如果我导航到 /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 路线,它会转到 Opportunities#index 操作。 P/S:对不起,我忘了提到我有类似的 index 操作路线。
  • 如果您的 Opportunities#index 路线是 /oportunidades/alquiler/san-miguel-de-tucuman,/oportunidades/alquiler/san-miguel-de-tucuman?id=123456 将始终触发 index 方法,因为 ?id=123456 是一个查询字符串参数,而不是 rails 路线参数。添加您的索引路线以准确了解 Rails 对每种情况的作用。
猜你喜欢
  • 2011-05-06
  • 1970-01-01
  • 2013-11-02
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多