我有以下代码:
<%= 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
答案 0 :(得分:0)
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
books / index.html.erb 中的3)
<% @books.each do |book| %>
<%= book.name %><br>
<% end %>