the model like this:
create_table "user_accounts", :force => true do |t|
t.string "code"
t.string "user_name"
t.integer "user_type", :default => 1
end
the controller’s code like this:
def index
@user_accounts = UserAccount.all
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @user_accounts }
format.xml { render :xml => @user_accounts }
end
end
The View’s code like this:
<table>
<tr>
<th><%= t :code %></th>
<th><%= t :user_name %></th>
<th><%= t :user_type %></th>
<th></th>
<th></th>
<th></th>
</tr>
<% @user_accounts.each do |user_account| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
<td><%= user_account.code %></td>
<td><%= user_account.user_name %></td>
<td><%= user_account.user_type %></td>
<td><%= link_to 'Show', user_account %></td>
<td><%= link_to 'Edit', edit_user_account_path(user_account) %></td>
<td><%= link_to 'Destroy', user_account, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
Everything works fine. but there is a flaw that the ‘user_type’ displayed as a number. but i hope it can display as string like ‘normal user’ or ‘system admin’.
The most important thing that I DON’T want to add any logic in the view(index.html.erb).
So what i need is to change the user_type’s value at the controller or wherever.
there must be some elegant ways to do it. But i don’t know.Hope you guys can give me some suggestion. Thanks!
You can add to you model UserAccount some function like this
And this method you can use in the view