If else statements in .html.erb in views
Solution 1
Unless you can think of a way to re-write this as a helper method, you're basically stuck with it looking kind of ugly. That's just how ERB is, as it was intended to be a minimal way of injecting Ruby into an otherwise plain-text template, not as something necessarily streamlined or elegant.
The good news is a syntax-highlighting editor will usually make your <% ... %>
ERB blocks look visually different from your HTML so that can dramatically improve readability.
It's also why other representations like HAML have been created where that syntax is a lot less cluttered:
- if some_condition_previusly_established_in_a_controller
.one 123
- else
.two something else
Solution 2
For one or two such conditional logic in your views, I guess its fine but when your code gets bigger and you have multiple if..else..end
and looks "cluttery", I think you should look at implementing "Presenter Pattern" which greatly cleans up your views by separating your logic to Presenters.
Here is a great tutorial I followed from Ryan Bates in his Rails Casts series on "Presenter Patterns from scratch". http://railscasts.com/episodes/287-presenters-from-scratch.
Solution 3
Have you tried?
<% @some_condition_previusly_established_in_a_controller ? <div class="one">123</div> : <div class="two">something else</div> %>
Solution 4
If your view contains lots of tags and HTML elements, you can put them into partials and logic into model
View:
<%= render :partial => @model.status %>
<%= render :partial => "file/path/#{@model.status}" %> # if your partial is in some different folder
If your status is one, then it would render the file _one.html.erb
If it is two, then it would render the file _two.html.erb automatically.
Model:
def status
if @some_condition
"one"
else
"two"
end
end
Solution 5
Yes, that is the standard (and yes, it looks cluttery).
If you're looking for a possibly cleaner alternative, check out: Conditional tag wrapping in Rails / ERB
dsp_099
Updated on October 27, 2020Comments
-
dsp_099 over 3 years
In rails, I often run into the situation where inside the views I'll do something like
<% if @some_condition_previusly_established_in_a_controller %> <div class="one">123</div> <% else %> <div class="two">something else</div> <% end %>
It looks a bit cluttery. Is this an acceptable way of working with views or not?
-
PJP almost 11 yearsThat sort of stuff is why I prefer HAML over ERB. HAML gets rid of the clutter.
-
Charles Caldwell almost 11 yearsNice link but not viewable to those of us without a paid account. =(
-
vee almost 11 yearsYes, I'm just concerned if I will be violating the copyright laws if I posted the github source link, I'll try to post a link to the source and edit my answer to include the necessary classes and files to look at if it doesn't chase me :)
-
Charles Caldwell almost 11 yearsIt looks like episode 287 is public in at least one of his repos.
-
vee almost 11 years@CharlesCaldwell, yes the link you provided does contain the implementation of Presenter Pattern. The files you want to be looking for are in the
app/presenters/
,app/helpers/application_helper.rb
and one of the usage of the same can be found inapp/views/users/show.html.erb
. Have a look at them inprofile-after
. -
hqt over 6 yearsI have a problem with HAML (I use slim). HAML syntax is clear and concise when your html code is short. but it will become messy when code is large. You also cannot format code when using some IDE or plugins ....
-
tadman over 6 years@hqt HAML is pretty good until your inline bits of code get too huge, it's true, but you can usually mitigate that by writing helper functions that minimize how much code you have in your template. As you point out there are other HAML-like meta-HTML notation systems as well, so if you're unhappy with ERB you do have options.