ruby-on-rails – 在Rails中带有可选参数的路由
发布时间:2020-12-17 02:14:27 所属栏目:百科 来源:网络整理
导读:我正在尝试设置一个看起来像这样的路线:acme.com/posts/:category /:status.两者:category和:status是可选的.我写了很多变化,但都没有奏效: resources :posts do match '(/:category)(/:status)',to: 'posts#index',as: 'filter',on: :collectionend# C
我正在尝试设置一个看起来像这样的路线: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",:draft) 我们的想法是能够1)按类别过滤,2)按状态过滤,3)如果两者都可用,则按类别和状态过滤.当前的设置也打破了我的/ posts / new路径,总是重定向到post #index. 解决方法
您可以使用更多RESTful资源:posts(在config / routes.rb中)并在查询字符串中发送params.
使用该方法,所有参数都是可选的,您不限于使用预定义参数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |