Rashed Al-Julaibi
Rashed Al-Julaibi

Reputation: 281

How to change html class dynamically in a link_to helper

I have the following link_to helper:

 <div class="button_wrapper"><%= link_to "View Course", [course.grade, course], :class => "course_button" %></div>

and I would like to be able to change the :class => "course_button" depending on which Subject the Course belongs to which I can specify when creating a new course. So basically I want the button to change color depending on which Subject it belongs to, for example Math will be blue and Chemistry will be green. The way im aiming to achieve this (which may not be the best way) is to create different styling in css for each subject, and then I would like the Subject name to be inputed dynamically in the :class, so the end result will be something like this if I choose maths:

<div class="button_wrapper"><%= link_to "View Course", [course.grade, course], :class => "Math_button" %></div>

How can I achieve this?

Upvotes: 0

Views: 994

Answers (1)

Muhammad Ali
Muhammad Ali

Reputation: 2183

I would be like this :

<div class="button_wrapper"><%= link_to "View Course", [course.grade, course], :class => "#{course.subject.name}_button" %></div>

Upvotes: 2

Related Questions