gpt4 book ai didi

ruby-on-rails - Rails 中带有可选参数的路由

转载 作者:行者123 更新时间:2023-12-04 07:40:03 24 4
gpt4 key购买 nike

我正在尝试设置一条如下所示的路线:acme.com/posts/:category/:status .两者 :category:status是可选的。我写了很多变体,但没有一个奏效:

resources :posts do
match '(/:category)(/:status)', to: 'posts#index', as: 'filter', on: :collection
end

# Category Links
link_to "Questions", filter_posts_path(:questions)
link_to "Suggestions", filter_posts_path(:suggestions)

# Status Links
link_to "Published", filter_posts_path(params[:category], :published)
link_to "Draft", filter_posts_path(params[:category], :draft)

这个想法是能够1)按类别过滤,2)按状态过滤,3)如果两者都可用,则按类别和状态过滤。当前的设置也破坏了我的 /posts/new路径,总是重定向到 posts#index .

最佳答案

您可以使用更多的 RESTful resources :posts (在 config/routes.rb 中)并在查询字符串中发送参数。

使用这种方法,所有参数都是可选的,您不仅可以使用预定义的参数。

关于ruby-on-rails - Rails 中带有可选参数的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13075461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com