使用参数从命名路由生成自定义 uri(或 url)
在 Rails3 应用程序上,从 Rake 任务中,我想生成集合页面的 uri 路径(具有由 params[:page] 设置的分页),并且我希望结果为:
- "/mycustomname"
- "/mycustomname/ 1"
- "/mycustomname/2"
- "/mycustomname/n"
然后我根据我的需要在routes.rb上设置了自定义规则:
get 'mycustomname/:page' => 'mycontroller#myaction', :constraints => { :page => /\d+/ }, :as => "myelement"
get 'mycustomname' => 'mycontroller#myaction', :as => "myelement"
然后当我从控制台尝试它时,例如:
app.myelement_path(:page=>3)
我检索:
- “/mycustomname?page=3”
而不是
- “/mycustomname/3”
要获得我想要的结果,缺少什么?
on a Rails3 app, from a Rake task, i want to generate a uri path of a collection page (with a pagination set by the params[:page]), and i wish the outcome had:
- "/mycustomname"
- "/mycustomname/1"
- "/mycustomname/2"
- "/mycustomname/n"
then i have setup the custom rule on routes.rb, according to my needs:
get 'mycustomname/:page' => 'mycontroller#myaction', :constraints => { :page => /\d+/ }, :as => "myelement"
get 'mycustomname' => 'mycontroller#myaction', :as => "myelement"
then when i try it from console, for example:
app.myelement_path(:page=>3)
i retrieve the:
- "/mycustomname?page=3"
instead of
- "/mycustomname/3"
What is missing to get the result I want?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试按字面意思传递页码:
app.myelement_path(3)
Try passing the page number literally:
app.myelement_path(3)