[英]How to link to users/id/microposts in Ruby on Rails?
I need create a link to users/id/microposts
where id
is the user id. 我需要创建一个指向
users/id/microposts
的链接,其中id
是用户ID。 I was wondering how I could do that with link_to
? 我想知道如何使用
link_to
做到这一点?
The HTML code is I am using for the moment is : 我目前使用的HTML代码是:
<strong><a href="/users/<%= @user.id %>/microposts">Microposts</a></strong> <%= user.microposts.count %>
Which renders the following : 呈现以下内容:
<a href="/users/1/microposts">Microposts</a>
Assuming that you have your routes setup correctly, that would be a nested route for the microposts, In rails 3 it would be 假设您正确设置了路线,那将是微桩的嵌套路线,在rails 3中将是
resources :users do
resources :microposts
end
Then in your view, with link_to you can 然后在您看来,使用link_to您可以
<%= link_to "Microposts", user_microposts_path(user) %>
This might be current_user, @user ... whatever user object you want the microposts, it might be from 这可能是current_user,@ user ...无论您想要哪些微博用户对象,都可能来自
@users.each do |user|
<%= link_to "Microposts", user_microposts_path(user) %>
end
OR 要么
<%= link_to "Microposts", user_microposts_path(current_user) %>
EDIT: Added in the user object I forgot to passin and some examples with it. 编辑:在我忘记传递的用户对象中添加了一些示例。
In routes.rb, you should have something like this: 在routes.rb中,您应该具有以下内容:
match '/users/:id/microposts' => 'microposts#index_by_user', :as => :microposts_by_user
and in view, you can do like this: 并且鉴于此,您可以这样做:
<%= link_to "Microposts", microposts_by_user_path( @user) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.