简体   繁体   English

带有自定义参数的Rails URL助手

[英]Rails url helper with custom params

There is the following code: 有以下代码:

before { get user_posts_path(user) }

As you can see I'm trying to make a 'GET' request, but this helper (user_posts_path) doesn't work, because I need to use user.login insteaf of user.id for this helper. 如您所见,我正在尝试发出“ GET”请求,但是此助手(user_posts_path)不起作用,因为我需要使用user.login insteaf user.id作为此助手。 How can I fix it? 我该如何解决? Thanks. 谢谢。

If user.login is just a value, you should be able to use the helper as 如果user.login只是一个值,那么您应该可以使用helper作为

user_posts_path(user.login)

If you only ever need user.login in the url (instead of id), you can override the method to_param on User to return the login instead of the id. 如果您只需要url中的user.login(而不是id),则可以覆盖User的to_param方法以返回登录名而不是id。 The ActiveRecord::Base version is deprecated (moved to another module), but this page lists the other versions: 不推荐使用ActiveRecord :: Base版本(移至另一个模块),但是此页面列出了其他版本:

http://apidock.com/rails/ActiveRecord/Base/to_param http://apidock.com/rails/ActiveRecord/Base/to_param

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM