activestorage user.avatar.attached? in javascript

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

activestorage user.avatar.attached? in javascript

fugee ohu
How would I run this same test in javascript without renaming my room_channel.js to room_channel.js.erb ?

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: activestorage user.avatar.attached? in javascript

Ariel Juodziukynas
You can't, you need the .erb extension to execute ruby code.

El lun., 27 ene. 2020 a las 0:29, fugee ohu (<[hidden email]>) escribió:
How would I run this same test in javascript without renaming my room_channel.js to room_channel.js.erb ?

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcA4umMPFHLoXCdEZHa0SegVHt3hXGKYt5HfK4LUiiBzAA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: activestorage user.avatar.attached? in javascript

fugee ohu


On Monday, January 27, 2020 at 8:28:48 AM UTC-5, Ariel Juodziukynas wrote:
You can't, you need the .erb extension to execute ruby code.

El lun., 27 ene. 2020 a las 0:29, fugee ohu (<<a href="javascript:" target="_blank" gdf-obfuscated-mailto="5xJKOiQpEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...>) escribió:
How would I run this same test in javascript without renaming my room_channel.js to room_channel.js.erb ?

--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="5xJKOiQpEgAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com?utm_medium=email&amp;utm_source=footer" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com?utm_medium\x3demail\x26utm_source\x3dfooter&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com.

I can change any .js file to .js.erb ? In this case it's app/javascript/channels/room_channel.js Passing data between javascript and rails is an important topic so I wanna learn as best I can without "cheating" but I'm attracted to the js.erb method too, I wanna mix it up something use js.erb sometimes use just .js so I don't end up ignorant

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3866a144-220f-4e6a-ad06-090226af6ef9%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: activestorage user.avatar.attached? in javascript

Ariel Juodziukynas
Although you can use .js.erb extension for any .js file you have to understand WHEN that erb gets executed to understand what you can do with it.

Assets with .erb extension are compiled to JS code during the assets precompilation process, you can't put request dependant code (current_user, instance variables, request controller/action, etc) inside an .js.erb asset file because it's an isolated process (assets are compiled into one final static .js bundle).

You can put request dependant code on .js.erb VIEWS because views are executed when they are needed for each request.

Not all js files are used the same way, you can use .erb extension to have ruby code but have to take into account the context to understand what variables you have access to.

El mar., 28 ene. 2020 a las 11:17, fugee ohu (<[hidden email]>) escribió:


On Monday, January 27, 2020 at 8:28:48 AM UTC-5, Ariel Juodziukynas wrote:
You can't, you need the .erb extension to execute ruby code.

El lun., 27 ene. 2020 a las 0:29, fugee ohu (<[hidden email]>) escribió:
How would I run this same test in javascript without renaming my room_channel.js to room_channel.js.erb ?

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com.

I can change any .js file to .js.erb ? In this case it's app/javascript/channels/room_channel.js Passing data between javascript and rails is an important topic so I wanna learn as best I can without "cheating" but I'm attracted to the js.erb method too, I wanna mix it up something use js.erb sometimes use just .js so I don't end up ignorant

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3866a144-220f-4e6a-ad06-090226af6ef9%40googlegroups.com.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcAKuKSx6WOsJibuiFqe8MmJvTh4N%2B%3D6WUpmFJ3u20CMDA%40mail.gmail.com.
Reply | Threaded
Open this post in threaded view
|

Re: activestorage user.avatar.attached? in javascript

Ariel Juodziukynas
In your case, you CAN'T call "user.avatar.attached?" inside app/javascript/channels/room_channel.js.erb because it's a request dependant object.

El mar., 28 ene. 2020 a las 12:24, Ariel Juodziukynas (<[hidden email]>) escribió:
Although you can use .js.erb extension for any .js file you have to understand WHEN that erb gets executed to understand what you can do with it.

Assets with .erb extension are compiled to JS code during the assets precompilation process, you can't put request dependant code (current_user, instance variables, request controller/action, etc) inside an .js.erb asset file because it's an isolated process (assets are compiled into one final static .js bundle).

You can put request dependant code on .js.erb VIEWS because views are executed when they are needed for each request.

Not all js files are used the same way, you can use .erb extension to have ruby code but have to take into account the context to understand what variables you have access to.

El mar., 28 ene. 2020 a las 11:17, fugee ohu (<[hidden email]>) escribió:


On Monday, January 27, 2020 at 8:28:48 AM UTC-5, Ariel Juodziukynas wrote:
You can't, you need the .erb extension to execute ruby code.

El lun., 27 ene. 2020 a las 0:29, fugee ohu (<[hidden email]>) escribió:
How would I run this same test in javascript without renaming my room_channel.js to room_channel.js.erb ?

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bdf21e73-81a0-4485-957e-82afa0e2d6a9%40googlegroups.com.

I can change any .js file to .js.erb ? In this case it's app/javascript/channels/room_channel.js Passing data between javascript and rails is an important topic so I wanna learn as best I can without "cheating" but I'm attracted to the js.erb method too, I wanna mix it up something use js.erb sometimes use just .js so I don't end up ignorant

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3866a144-220f-4e6a-ad06-090226af6ef9%40googlegroups.com.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAPS3bcAct%3Do%2BwsthvgLRLb2rx5wONGx4Zznh1bDqMYb7%3D%3DYYMQ%40mail.gmail.com.