I have a question about using link_to in template and in console
Say I do rails new blah, I make a model called User, I make a table users, each user has a field called ‘name’. I have resources :users, in config/routes.rb And I add some users.
in template, I can do
<% @z=User.find_by(name:“bob”) %>
<%= link_to ‘aaa’,@z %>
I understand that it will take that variable @z which is a reference to a user, and will convert it to user_path(@z.id)
And I can do for example
<%= link_to ‘aaa’,user_path(@z.id) %>
And in the console, I can say
But I notice that in the console I can’t do that shorthand as shown above with @z
I can say
=> “<a href=”/users/4">aaa"
#<User id: 3, name: “bob”>
Traceback (most recent call last):
1: from (irb):5
ArgumentError (arguments passed to url_for can’t be handled. Please require routes or provide your own implementation)
And also, I notice that in the template, while I can say user_path(@z.id), I can’t say app.user_path(@z.id)
Why is it that in the console, I can’t use that shorthand of writing @z or user, in a link_to, with that variable as an argument, when that variable points to a user. Whereas I can in a template.
And why is it that in a template, I can’t refer to app.user_path only to user_path ?