Rails 3 link_to 问题
假设我的routes.rb 文件中有以下路由 -
get 'site/user/:id' => 'users#show', :as => :get_user
如何使用 link_to 使用命名路由创建指向特定用户 ID 的链接:get_user
# so this will spit out "site/user", but i want /site/user/23 as output
link_to 'Some User', :get_user
注意 -:我不想映射用户作为我的路线文件中的资源。另外,“用户”对象是一个哈希值,而不是我的用户模型的实例。
现在这就是我所拥有的。正在寻找一种更清洁的方法,有吗?
link_to 'Some User', {:controller => 'users', :action => 'show', :id => "#{user[:id]}"}
我在 Rails v3.0.3
So lets say I have the following route in my routes.rb file -
get 'site/user/:id' => 'users#show', :as => :get_user
How do I use link_to create a link to a particular user id by using named route :get_user
# so this will spit out "site/user", but i want /site/user/23 as output
link_to 'Some User', :get_user
NOTE -: I dont want to map user as a resource in my routes file. Also "user" object is a hash not an instance of my User model.
For now this is what I have. Looking for a cleaner approach, is there any?
link_to 'Some User', {:controller => 'users', :action => 'show', :id => "#{user[:id]}"}
I am on Rails v3.0.3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
get_user_path(user[:id])
应该可以工作,因为您使用的是哈希而不是 ActiveRecord 模型。get_user_path(user[:id])
should work since you're using a hash rather than an ActiveRecord model.get_user_path(user)
应该可以工作。(假设
user
是您的 User 模型的实例。)get_user_path(user)
should work.(Assuming
user
is an instance of your User model.)