Basically what I am doing here is keeping track of 4 different types of ratings
. If any rating is present, I would like to apply a different css class to that rating and then apply a default css class to the other spans rendered in that line.
ratings = ["speed", "tackling", "passing", "dribbling"]
ratings.each do |rating|
content_tag :div, class: "col-lg-3" do
if rating_param.eql? rating
if rating.eql? "speed"
content_tag :span, class: "label label-success label-lg" do
"#{rating.capitalize}: #{profile.ratings.find_by(user: current_user)[rating]}"
end
elsif rating.eql? "tackling"
content_tag :span, class: "label label-tackling label-lg" do
"#{rating.capitalize}: #{profile.ratings.find_by(user: current_user)[rating]}"
end
end
else
content_tag :span, class: "label label-default" do
"#{rating.capitalize}: #{profile.ratings.find_by(user: current_user)[rating]}"
end
end
end
end
So basically what I need to do is map the rating
to the class.
For instance, it may look like this:
speed: success, tackling: info, dribbling: primary, passing: warning
.
So if the rating is speed
, it should apply the class success
, and so on.
How do I refactor this so it isn't a bunch of ugly if
statements like this?
content_tag
call. - Dave Newtoncontent_tag
is a Rails' method, so you need to add a Rail's tag. - Cary Swoveland