我有以下代码:
<%= link_to new_book_path(controller: :books, action: 'new', id: comment) %>
#also tried:
<%= link_to new_book_path(comment.user.id) %>
#outputs: undefined id
<%= link_to new_book_path(comment.user_id) %>
#leads to my (logged-in user) book list, not this user's
<%= link_to new_book_path(comment.user) %>
#same
<%= link_to new_book_path(comment) do %>
#same. comment.post.book.user.id also same.
我想知道如何通过该用户评论中的link_to访问该特定用户的书籍列表。我继续去我自己的。
我的路线是:
resources :books do
resources :posts, shallow: true
end
resources :posts do
resources :comments, shallow: true
end
resources :users do
resources :comments, shallow: true
end
1( 在注释中:
<%= link_to comment.user.name, books_list_path(user_id: comment.user.id)
books_list_path是列出书籍的路径
2( 在列出书籍的操作中(索引或新操作(:
class BooksController < ApplicationController
..
def index
@books = Book.where(user: params[:user_id])
end
..
end
3( 在书籍/索引.html.erb
<% @books.each do |book| %>
<%= book.name %><br>
<% end %>