Stop array of strings from saving with dashes
I'm listing the models in my app so an admin can create custom roles:
<% ActiveRecord::Base.send(:subclasses).each do |model| %>
<tr>
<td width=10>
<label><%= check_box_tag "role[read_models][]", model.name, @role.read_models.include?(model.name) %></label><br />
</td>
<td width=10>
<label><%= check_box_tag "rol开发者_JS百科e[write_models][]", model.name, @role.write_models.include?(model.name) %></label><br />
</td>
<td><%= model.name %></td>
</tr>
<% end -%>
It works great by the way. In the log it saves the array properly like so:
"read_models"=>["Slug", "Account", "Category", "Document", "Group", "Location", "Role", "Status", "Task", "Ticket"]
But when outputting the results:
<%= @role.read_models.each do |model| %>
<%= model %><br />
<% end -%>
I get this:
---
- Slug
- Account
- Category
- Document
- Group
- Location
- Role
- Status
- Task
- Ticket
(Including the three dashes in the front)
I've tried doing to_a.join(', ') but it still has the dashes in front of each one.
Any ideas on how I need to change this process? Thanks!
I am guessing you have a Role class and this class is the one used to store these values, if this is your case, here's what you could do:
class Role < ActiveRecord::Base
serialize :read_models, Array
end
This will make ActiveRecord store these values you have at the read_models column as a YAML representation (this one you already have) but then you make @role.read_models you will get the Array back and not a string containing the YAML representation.
精彩评论