Reputation: 1165
Ok, so in my user model i have created get_username in an attempt to format the username.
The idea is to be able to use
TheUser.get_username
and it would correctly format the username based on user level (admin). As a test I created this:
def get_username
fname = '<span style="color:red">John</span>'
fname
end
However, it displayed the value literally with the code showing..
How would i be able to plug the username in as well as display correctly
Upvotes: 0
Views: 141
Reputation: 740
The Model is not supposed to create the html. You do that in the views. May I suggest another approach instead:
I suppose, you got some variable to store the user_level
. Use this in the view like so:
# .ERB-File
<span class="<%= user.user_level %>"><%= user.user_name %></span>
Now format it with css:
# .css-File
span.normal_user {
color: green;
}
span.admin {
color: red;
}
PS: I'm not sure about the ERB-Syntax (I use haml).
Upvotes: 0
Reputation: 3607
You can use html_safe
.
def get_username
fname = '<span style="color:red">John</span>'
fname.html_safe
end
To get the user_name use:
def get_username
fname = "<span style='color:red'>#{user_name}</span>"
fname.html_safe
end
Note that this is very bad code, not only is this abusing the seperation of responsibilty of MVC it also may introduce security vulernabilities.
Code like this should be in view or a helper.
Upvotes: 1