How to use if statement to avoid accessing a file?

Viewed 52

I'm pretty new to Ruby. I have a situation where I'm dealing with dashboard. I have 3 types of metrics (ex- 1,2,3) present in the dashboard where the code for three different metrics are present in 3 different files. Now, I want to write an if statement where the condition should be similar to the following...

<% if person=x %>
Do not access the file which has metric 2 / the filename
<%end%>

How can I write " Do not access the file which has metric 2 / the filename " in code format?

However I want the rest other files 1 &3 to be accessed by person=x. Any leads would be helpful. Thank you!

P.S - All the files are in .html.erb format

1 Answers

I could be more specific if you share more of your code for the dashboard. But you can write the code for your file access and put it in a guard clause:

<% unless person == xxx %>
   <%= link_to 'some file', some_file_url %>
<% end %>

If you flesh out your question with more of your own code I could help refine this answer.

You could always just do:

<% if person != xxx %>
   <%= link_to 'some file', some_file_url %>
<% end %>

But Ruby gives us unless for situations where it might be more expressive than a negative like !=. Depending on your use, you could even probably shorten it to:

<%= link_to('some file', some_file_url) unless person == xxx %>
Related