简体   繁体   中英

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. How can I fix it? Thanks.

If user.login is just a value, you should be able to use the helper as

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. The ActiveRecord::Base version is deprecated (moved to another module), but this page lists the other versions:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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