jeudi 21 janvier 2021

Data which user saved make the initial value of checkbox

Background

In my app, users can choose multiple languages which they're able to speak on their profile edit page (users/edit.html.erb) from checkbox. Many-to-many relation is already written, so multiple data can be saved properly.

Now, when users came back to their profle edit page, all the checkbox are displayed in a blank. I would like to display languages with "checked" which user chose and saved.

users/edit.html.erb

<li>
 <div class="col-1">Spoken languages:</div>
   <div class="col-2">      
     <label><input type="checkbox" name="language[]" value="1">Japanese</label>
     <label><input type="checkbox" name="language[]" value="2">English</label>
     <label><input type="checkbox" name="language[]" value="3">Chinese</label>
     <label><input type="checkbox" name="language[]" value="4">Spanish</label>
  </div>    
</li> 

What I tried to do

I searched this kind of function, but I only found how to make a specific initial value (such as always check "English"). I would be grateful if someone give me advice.

Version

ruby 2.6.4p104 RubyGems 3.0.3 Rails 5.2.3




Aucun commentaire:

Enregistrer un commentaire