Curated list of episodes, bundled to help you on your learning path!
Videos of tech, infrastructure, hardware and software which non-instructional by nature.
Articles around software and tutorials.
Show your love and support while looking fancy.
Got a question or want to chat about a topic? Let's talk!
Thanks for providing the JS equivalent code!
Great work, thank you!
I have implemented the controller action specific CoffeeScript approach. Therefore I have 1 .coffee file per controller action. However I stumbled upon an issue... let's say I have a controller with a 'new' action and a respective 'new.coffee' script. When I submit the 'new' page that triggers Rails validation errors, the controller action gets updated to 'create', since it was a Post. Therefore 'new.coffee' won't execute while my controller renders 'new.html.erb' directly, not through a redirect, as is standard Rails scaffold.
In this case what would you recommend? Should I simply use the 1 file per controller approach?
You could mix and match based on the complexity of the JS needed. You could also look at something like client side validations to prevent the form from posting if the data isn’t valid (but not recommended for uniqueness validations because of potential security reasons).
Got it! Thank you :D
Thanks for this, I have spent a lot of time trying different approaches of this for our app :).
Our app has 23 different controllers, each with several actions. Navigating all of those folders and different files, if we used this setup would be a pain.
I do think that people and companies should do what they feel is best for them. Though, the same logic can apply to just controllers. Also, you wouldn’t have to create a file for every controller and/or action, just the ones you would want PSJ.
However, I wouldn’t put the JS directly in the views for a few reasons.
In the view, the JS will not be minified or compressed.
The CDN likely won’t cache the results of the page and cause extra bandwidth and load times (minimal but still).
You lose access to the reuse of JS.
Does page-specific-js-in-ruby still make sense, since we have now Stimulus js ?