1

I have the following code:

<%= 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.

I was wondering how I can get to this particular user's book list through link_to from this user's comment. I keep going to my own.

My routes are:

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
  • 4,591
  • 2
  • 22
  • 30
superbot
  • 401
  • 3
  • 19
  • You want a link to a new book form? Or do you want a book link from the comment? – demir Aug 14 '19 at 21:20
  • @demir So the book list lives in new_book_path (confusing, sorry). On that url is a list of books already created. I want to go there by clicking on this user's name on this user's comment. – superbot Aug 14 '19 at 21:26
  • Can you show your controller for this view? – Mosaaleb Aug 14 '19 at 21:35
  • @Mosaaleb Sure, thank you. This particular view is index for posts: ```@posts = Post.all``` and ```@comment = Comment.new```. The url I'm trying to get to is new for book: ```@user = current_user // @book = Book.new``` – superbot Aug 14 '19 at 21:39

1 Answers1

0

1) in comment:

<%= link_to comment.user.name, books_list_path(user_id: comment.user.id)

books_list_path is path where books are listed

2) in action where books are listed (index or new action):

class BooksController < ApplicationController
  ..
  def index
    @books = Book.where(user: params[:user_id])
  end
  ..
end

3) in books/index.html.erb

<% @books.each do |book| %>
  <%= book.name %><br>
<% end %>
demir
  • 4,591
  • 2
  • 22
  • 30
  • If it doesn't work, can you share the related models and views? – demir Aug 14 '19 at 21:47
  • Thank you for this. To clarify, I am trying to go from (A) a user's comment on another user's (or this user's) post (on index.html.erb of Posts controller) to (B) that user's list of books (on new.html.erb of Books controller). On each book show.html is a list of posts with comments on each post. The url is /books/new (using devise). Perhaps that's where the trouble is? There is no difference between users in their urls, because it's all books/new. – superbot Aug 14 '19 at 22:23
  • It is correct to use **index** action to list books. Using **new** action for listing is not correct. And can you show the book model? – demir Aug 14 '19 at 22:42
  • I see, thank you. Sure: ```belongs_to :user``` ```has_many :posts, dependent: :destroy``` – superbot Aug 14 '19 at 22:44
  • And each comment is related to user. ```belongs_to :user``` ```belongs_to :post``` – superbot Aug 14 '19 at 22:45
  • Thank you so much! I will try later today. – superbot Aug 15 '19 at 00:03
  • Doesn't work, but thank you again. I will try some new stuff... will update. – superbot Aug 16 '19 at 18:58
  • I got some of it to work, but one thing stands in the way. I'd appreciate it if you could help with how to write this in the routes.rb: ```books_path(user_id: current_user)```. This is necessary to show books on books/index, because following your suggestion (which worked after I changed some code; I will write what those are by editing your answer later), all my books there disappeared. So I have to change the url to ```http://localhost:3000/books?user_id=1``` – superbot Aug 16 '19 at 19:26