In Ruby on Rails is there a way to put the path of every partial in an HTML comment? - ruby-on-rails

I work on apps that often have thousands of partials and finding what partial is rendering a section of HTML can be tedious and just waste time.
In development mode, it would sometimes be very helpful to turn on a config to have every partial used be prefaced with its path in an HTML comment so I could quickly know how to access and edit it.
Is there a config option for this or a gem that someone has made for this?

This functionality is built-in to recent rails versions for erb templates.
See here for details: https://blog.saeloun.com/2020/05/11/rails-support-annotates-html-output-with-template-file-names.html
That page mentions another option you could try.
Here is a link to the rails pr that introduced the change: https://github.com/rails/rails/pull/38848

Related

What are the reasons to use erb integration for webpacker in Rails 6?

I have surfed a couple of hours through the web but couldn't find any articles/walkthroughs/comparisons touching erb integration of webpacker. I've found 1 question, unfortunately, the author haven't read docs attentively and the answer was right there, so - no any additional info there.
I have seen plenty of articles about vue and react, but nobody says a word about erb. However, it's quite clear why using react/vue/else similiar, it is not with erb.
The theme is quite vast and I expect a little hate towards me, so I'd ask two related questions (but if you have something to tell more about it - that's appreciated).
As I understand - it's vanilla (plain) js (maybe with a flavour of jQuery) caring just about dom and styling, with all the preprocessing made by rails. If it is so why not just continue using sprockets?
And what are the reasons to choose it instead of some react/vue/else framework?
You may use both : a vanilla JS framework (React, Vue ...) and some erb files. I find it interesting to setup my constant and other configuration variables within a .js.erb file that is generated by my Rails app when building the js app.
Things I like to put in this erb files :
schemas of my api, generated by my serializers
constants, like enum
values to be used in forms
To generalize, you can put anything owned by the backend that will not change at run time
this save you a couple API calls to retrieve this data. However, I tend to stop doing this as your JS app and Rails become tightly coupled and you can't use the sources of your JS app outside the Rails app

Find url for webpage based on a partial file

I was introduced to a large team and not very familiar with the site I am developing for. I need to make an edit to a partial (i.e. _partial_file.html.haml) and need to verify the change visually but I don't know where that partial is being displayed on the website. Is there a way I can get a url from a partial file with rails.
For example, could I run a rake routes | grep or do something in the rails console to help find any URL where the partial is displayed?
Thanks
If I understand you correctly. While in your rails app root.
grep -r "partial_file" .
This will allow you to look for instances of partial_file and where it's being rendered. You might see it in a view or a controller. Depending on where you find it, you can rake routes and see just what url you should enter to see your output
When you render a page you should see output in the console for all templates being rendered.
example rendering template show.html.haml from SomeController#index
Use this to locate the template that is including your partial. You can then use that to search the entire project for all templates including that partial.
Rails renders templates by a specific convention that maps controller methods to specific layouts and templates. I would familiarize yourself with http://guides.rubyonrails.org/layouts_and_rendering.html to get a better idea of how the default internals work for you rails app and use that as a starting point.

Locomotive CMS with Rails - location for liquid templates

I'm new to both Ruby on Rails and Locomotive CMS, but I'm just starting to create my first site with them.
I've got the engine running in a full Rails app (I'm going to need to deploy it on our own server later on). But it's just spitting out the 'Template' content defined through the admin interface, without any other template/content around it.
I can 'fix' it by shoving the html for the whole page in through this input field. But that's not right, surely? The Getting Started guide talks of putting the templates in the filesystem, at something like: Pages/index/first page. "All pages are inherited from index". I have an index.liquid under views/pages but it's not picking that up... (I've tried a couple of other locations too).
I'm sure this is a dumb question, but please could someone tell me where to put my template in the file system? Or how to point Locomotive to pick it up from the right place?
(I did get the file system liquid template working by defining it through the Rails way, with a route, a controller and adding a liquid template initializer I found here. But then it's missing the variables that should come from the CMS content).
I'm loading the site using bundle exec unicorn_rails. And I'm using Rails v3.2.13, Ruby v1.9.3 and Locomotive_cms v2.2.2.
Thanks!
I'm Didier from LocomotiveCMS.
LocomotiveCMS is a little bit different from the other CMS, in a sense, we offer a tool named Wagon to manage your site locally without having to install mongodb, rails and some other components.
Another huge benefit is that you can write your templates in HAML and your CSS in SASS/ SCSS or Less (we embedded Compass as well) and with our preferred texts editor (editing a whole site in a browser is a nightmare).
That's a nice eco-system in order to be super efficient when it comes to develop a LocomotiveCMS site.
Once you're done with your local work, you can deploy your site to a remote LocomotiveCMS engine in a similar way you push your application to Heroku. Actually, pushing a site will create the back-office for the final end user.
I suggest you to read that page.
http://doc.locomotivecms.com/guides/get-started/requirements
and this one too
http://www.locomotivecms.com/tour
Our message is still not clear on our official website but believe me, we are working to make it better.
Hope it will help you !
Didier

Rails HTML/TEXT UserMailer Templates

Currently for my Rails 3 mailer templates, I need to create an html and text version in the views/user_mailer directory.
Why is that necessary? Why can't rails look at the html version and automatically format it to a text/plain version?
This is not required. You can simply provide one of those templates. For instance, if you don't need HTML, you can create the TEXT file only.
Likewise, you can provide the HTML template only. Readers will attempts to extract the information.
However, if you use HTML, you should provide an alternative TEXT version to make sure the readers won't mess the content of your email.
By the way, Rails doesn't force you to provide both templates.
This question seems to be similar to yours and might be helpful to you:
Graceful degradation/progressive enhancement for Action Mailer templates?
Also, here are some direct links to some gems that help to solve this problem of duplication between html and text mailer templates:
https://github.com/plataformatec/markerb
https://github.com/Mange/roadie
https://github.com/fphilipe/premailer-rails3

Writing templates without using html for ruby on rails/django

I hate to explicitly use html/css to build pages. Is there a template language I can use to semantically and quickly describe the layout and content of the page so that later I can generate html and css from it?
I'm planning to use this on a django site (but I guess if such solution already exists for RoR I can always adapt it).
Thanks.
There's GRHML, which is HAML for Genshi. (Django adapter)

Resources