question about using link_to in template and in console

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

question about using link_to in template and in console

robert.phillips37
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 

>puts app.user_path(1)
/users/1

But I notice that in the console I can't do that shorthand as shown above with @z

I can say 

irb(main):003:0> helper.link_to("aaa",app.user_path(4))
=> "<a href=\"/users/4\">aaa</a>"
#<User id: 3, name: "bob">

irb> user=User.find_by(name:"bob")
irb(main):005:0> helper.link_to("aaa",user)
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)
irb(main):006:0> 

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 ?

Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/02beb053-f703-4611-99ce-8f6091d0655b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: question about using link_to in template and in console

localhostdotdev
actually it uses `to_param` to convert to the path (fallsback to id if not defined I think).

you could do something like app.instance_eval { link_to "bob", @bob } and app.instance_eval { link_to "bob", user_path(@bob) } etc.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b2f77cb6-4d6d-4aa5-9a2b-dea5a87c529b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: question about using link_to in template and in console

Ariel Juodziukynas
In reply to this post by robert.phillips37
I all comes down to the context where the code is executed. Rails console context (self) is the "main" object, while, in the view, the context is the view class which includes a lot of helpers.

The main object on the console has an "app" method, a "helpers" methods, etc, but the view class does not (it includes some methods on itself). That's why you can't call "app.something" on the view, there's no "app".

The case of the object as an argument is similar, link_to, under th hoods, uses "url_for", if you do app.url_for(@z) on the console you'll get the url but link_to is running on a different context (you are calling "helper.link_to", not just "link_to").

They are just different context with different available methods (check self.methods on both the console and when rendering a view an you'll see it's really diferent)

El mar., 16 abr. 2019 a las 0:22, Robert Phillips (<[hidden email]>) escribió:
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 

>puts app.user_path(1)
/users/1

But I notice that in the console I can't do that shorthand as shown above with @z

I can say 

irb(main):003:0> helper.link_to("aaa",app.user_path(4))
=> "<a href=\"/users/4\">aaa</a>"
#<User id: 3, name: "bob">

irb> user=User.find_by(name:"bob")
irb(main):005:0> helper.link_to("aaa",user)
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)
irb(main):006:0> 

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 ?

Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/02beb053-f703-4611-99ce-8f6091d0655b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcDE6R4tQihoTLLxvhLC4EgbOafzPzcvztNsxuE6oVvqUQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: question about using link_to in template and in console

robert.phillips37
In reply to this post by robert.phillips37
thanks local and ariel

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/5bb5642f-40d3-4e41-b080-4606a1e9e25b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.