Checking .html.erb file validity

1,001 views
Skip to first unread message

Tom Hale

unread,
Jul 20, 2016, 8:10:19 AM7/20/16
to Ruby or Rails Oceania
I'm want to check the validity of .html.erb files, at both the erb and
HTML levels.

I purposely put in weird, unmatched tags to test out rails-erb-lint[1] and
rails-erb-check[2]. I also changed a <% else %> to <% elXse %> and both
said my file was valid.

So...

Q1) How do I get feedback on what is actually wrong with my .erb syntax?

Q2) How can I validate that the eventual HTML output is also valid, and
not just relying on my browser being tolerant?

Q3) Do many people use erb in production or should I be using something
"better"? Something with a decent error checker? (bonus points for
integrating with vim/syntastic)

Thanks all,
Tom

[1] https://rubygems.org/gems/rails-erb-lint/versions/1.1.6
[2] https://github.com/jugyo/rails-erb-check

Andrew Grimm

unread,
Jul 20, 2016, 8:25:52 AM7/20/16
to rails-oceania
I use haml instead of erb, and haml-lint does a good of checking your code. It even uses RuboCop.

I'm not sure whether this'll help, but maybe try using a tool that converts your erb into haml, and then running haml-lint on the output.

Andrew



--
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rails-oceani...@googlegroups.com.
To post to this group, send email to rails-...@googlegroups.com.
Visit this group at https://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/d/optout.

Craig R Webster

unread,
Jul 20, 2016, 8:31:04 AM7/20/16
to rails-...@googlegroups.com
Hi Tom,

On 20 Jul 2016, at 12:37, Tom Hale <t...@hale.ee> wrote:

Q1) How do I get feedback on what is actually wrong with my .erb syntax?

Q2) How can I validate that the eventual HTML output is also valid, and not just relying on my browser being tolerant?

I think these could be achieved by having a test suite that covers your views. That could be focussed RSpec style tests, or it could be a higher level test such as you might get from Cucumber.

I've done something similar to this before, when ensuring the HTML was valid was important:


I'm pretty sure I ran a copy of the W3C validator locally to avoid going to the internet during the tests.

Q3) Do many people use erb in production or should I be using something "better"? Something with a decent error checker? (bonus points for integrating with vim/syntastic)

I'd guess (totally unsubstantiated) that ERB is the most used in production, since it's the default with Rails and there are a lot of older Rails apps still around.

What should you use if you're starting a project now? Talk to your team. Use what they can support and enjoy working in. If you're a team of 1, have a play and see which you prefer. I like ERB because it's the default, it's easy to see the HTML that'll be output, and just about everyone has used it or something similar - even if they're not Rails developers. HAML, Slim, Liquid, these are all good choices depending on your requirements and your team.

Hope that helps.

Cheers,
Craig | http://barkingiguana.com/
--
Barking Iguana Ltd. is a company registered in England and Wales.
Registered number: 08915147. Registered address: Jubilee House, East Beach, Lytham, St. Annes, Lancashire, England, FY8 5FT.


Ben Koshy

unread,
May 12, 2020, 11:03:21 PM5/12/20
to Ruby or Rails Oceania
Hi Tom

I found this gem:


The trick would be to integrate it into your view specs. I'm not sure if there's an easy way to do that, but at least there's a place to start.

regards
Ben
Reply all
Reply to author
Forward
0 new messages