lundi 23 septembre 2019

How to remember checkbox status Rails?

My app has a list of skills to choose from. When you click on 'Save', user id and skill id are recorded in the database.

checkboxes

If the user visits this page again, all checkboxes are turned off.

How to check from the database so that the selected checkboxes are always included in the view?

My view:

= form_for(:skill_list, url: user_skill_list_index_path) do |f|
 %li
  = f.check_box(:a1, class: 'm-enabled')
  = f.label(:a1, 'Programming')
%li
  = f.check_box(:a2, class: 'm-enabled')
  = f.label(:a2, 'Communication')
%li
  = f.check_box(:a3, class: 'm-enabled')
  = f.label(:a3, 'Problem-solving')
%li
  = f.check_box(:a4, class: 'm-enabled')
  = f.label(:a4, 'Teamwork')
%li
  = f.check_box(:a5, class: 'm-enabled')
  = f.label(:a5, 'Creative')
%li
  = f.check_box(:a6, class: 'm-enabled')
  = f.label(:a6, 'Marketing')

My controller:

def create
  @skill_record = UserSkillList.where(user_id: current_user.id).first
  ...
  if @skill_record.blank?
    UserSkillList.create(user_id: current_user.id, skill_id: skills)
  else
    @skill_record.update_attributes(skill_id: skills)
  end
  redirect_to :back
end




Aucun commentaire:

Enregistrer un commentaire