1

I have a ruby on rails application, and want to display an image on the index page depending on what the URL is. If the url is: localhost:3000/products/multi_find I do not want to show the image, if it is anything else I do want it to be shown.

Is their a way I can retrieve the url and store it in a variable to run an <& if statement like: if url != localhost:3000/products/multi_find &><%= image tag "test.png", :size => "15x18" %> <% end %>

Thank you in advance.

Roman Kiselenko
  • 43,210
  • 9
  • 91
  • 103

4 Answers4

1

None of those solutions worked, this did:

<% if request.original_url == 'http://localhost:3000/products/multi_find' %>
    <%= image tag "test.png", :size => "15x18" %>
<% end %>
0

In Rails you must think in terms of routing rules, not URLs. An URL is nothing else than the result of a routing directive.

Therefore, /products/multi_find can be expressed in terms of action, controller and parameters.

With that in mind, you can use the current_page? helper to check if the current URL matches the route you expect, if it does then ignore the image.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • Thank you, so would it be <% if current_page? != localhost:3000/products/multi_find %> –  Jan 18 '15 at 11:34
0

You can even use rails "action_name" and "controller_name" helper methods to get the job done.

rajesh023
  • 704
  • 1
  • 5
  • 15
  • Sorry I'm fairly new to ruby, can you show me how I'd use that please? Like where it goes, what the if statement would look like –  Jan 18 '15 at 12:32
  • you can refer this tag, http://stackoverflow.com/questions/5186613/rails-current-page-versus-controller-controller-name – rajesh023 Jan 18 '15 at 12:45
0

Try this:

<% unless current_page?('/products/multi_find') %>
  <%= image tag "test.png", :size => "15x18" %>
<% end %>
Thanus
  • 86
  • 4