Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby On Rails - HTML Conditional Based On Controller and Action

I have a syntax issue I'm trying to sort out. I'm just trying to check if a controller/action is loaded, and if so do something, and if not do something else, seems simple. This gives me an error:

<% if (:controller => 'home', :action => 'index') do %>
    <div class="header">
<% else %>
    <div class="header-2">
<% end %>  

Can someone assist me with the syntax issue here? Thank you!

like image 734
BradM Avatar asked Sep 22 '26 00:09

BradM


1 Answers

You have to modify the if clause like this:

<% if controller_name == 'home' && action_name == 'index' %>

Additionally, if have to call this more than once, I'd suggest you define a helper.

application_helper.rb

def home_index?
  controller_name == 'home' && action_name == 'index'
end

This way your code will be a lot more readable:

some_view.html.erb

<div class='<%= home_index? ? "foo" : "bar" %>'>
like image 196
davegson Avatar answered Sep 23 '26 14:09

davegson