【发布时间】:2019-08-14 20:57:40
【问题描述】:
我有以下代码:
<%= 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
【问题讨论】:
-
您想要一个指向新图书表单的链接?或者你想要评论中的书籍链接?
-
@demir 所以书单存在于 new_book_path 中(令人困惑,抱歉)。在那个 url 上是一个已经创建的书籍列表。我想在这个用户的评论上点击这个用户的名字去那里。
-
你能在这个视图中展示你的控制器吗?
-
@Mosaaleb 当然,谢谢。此特定视图是帖子的索引:
@posts = Post.all和@comment = Comment.new。我要访问的网址是新书:@user = current_user // @book = Book.new
标签: ruby-on-rails ruby link-to nested-resources