ruby on rails 3 - Any possible way to set radio_button_tag values by a database set value -
i have radio_button_tag in form, holds various values persons current availability:
mike donnall o available o out of office o vacation
so open form, , select value, sets value in status table person.
however, there's functionality re-open form , update present status, perhaps vacation available.
my question is, there anyway @ radio button :checked can modified accept custom method, have found in similar posting, want value foe radio button set value in db.
my code far, stab in dark perhaps:
view:
<% @people.each |p| %> <% @statuses.each |s| %> <%= "#{p.name}" %> <%= "#{s.status_name}" -%><%= radio_button_tag ['person', p.id], ['status', s.id], checked?(p.id) %> <% end %> <% end %>
helper:
def checked?(person) @person = person @status = status.find_by_sql(['select status_id statuses person_id = ?, @person]) if @result return true
end
as can see im bit lost here, understand method should return value of checkbox needs checked, im wondering because checked functionality, limited being true or false?
so persons.status.id check if true or false.
it seems helper's sql following relationship setup between people , statuses:
class person < activerecord::base has_one :status end class status < activerecord::base belongs_to :person end
you can access 1 given person status this:
person = person.first person_status = person.status
using knowledge, desired view outcome becomes quite simple:
<% @people.each |p| %> <p><%= "#{p.name}" -%> <% @statuses.each |s| %> <%= "#{s.status_name}" -%> <%= radio_button_tag ['person', p.id], ['status', s.id], (p.status == s) ? true : false %> <% end %> <% end %>
you can of course extract logic helper, doesn't seem necessary.
on personal note, isn't way i'd present information user, it' heavy on information in 1 line. suggest put person's name in p
tag, , use ul
tag statuses.
Comments
Post a Comment