Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form builder not working #22

Open
ausangshukla opened this issue Jun 21, 2022 · 6 comments
Open

Form builder not working #22

ausangshukla opened this issue Jun 21, 2022 · 6 comments

Comments

@ausangshukla
Copy link

ausangshukla commented Jun 21, 2022

The code to generate the form is not working
= f.collection_check_boxes :languages, Profile.languages.pairs

wrong number of arguments (given 2, expected 4..6)

Would there be a way to resolve this ? Thanks

@ausangshukla
Copy link
Author

ausangshukla commented Jun 21, 2022

I solved this manually as follows

<% User.permissions.pairs.each do |key, val| %>
  <div class="form-group">
    <%= form.label :permissions, key %>
    <%= form.check_box :permissions, {name: "user[permissions][]",  
        val: val, id: "permissions_#{val}", class: "form-control form-checkbox"}, val, nil   %>
  </div>
  <% end %>

@kenn
Copy link
Owner

kenn commented Jun 22, 2022

Hm, what Rails version are you using?

@ausangshukla
Copy link
Author

ausangshukla commented Jun 22, 2022 via email

@TheBrockEllis
Copy link

TheBrockEllis commented Nov 8, 2022

I am using rails 6.1 and was also running into the same problem. After reading the docs on the collection_check_boxes helper about 6 times, I noticed that they said:

The :value_method and :text_method parameters are methods to be called on each member of collection. The return values are used as the value attribute and contents of each check box tag, respectively. They can also be any object that responds to call, such as a proc, that will be called for each member of the collection to retrieve the value/text.

So I tried using a Proc in place of the value_method and name_method and was able to use that to extract the entries from the array.

<div class="field mb-4">
    <%= f.label :industries %><br />
    <%= f.collection_check_boxes :industries, Athlete.industries.pairs, Proc.new {|industry| industry[1]}, Proc.new {|industry| industry[0]}, html_options: { class: 'form-control w-100 w-lg-50' } %>
</div>

@kenn kenn reopened this Nov 20, 2022
@isabel-anastasiadis-boost

Another option I found is you can represent each pair as an array and use the :last and :first methods

<div class="field mb-4">
    <%= f.label :industries %><br />
    <%= f.collection_check_boxes :industries, [[:industry_1, "Industry 1"], [:industry_2, "Industry 2"]], :first, :last, html_options: { class: 'form-control w-100 w-lg-50' } %>
</div>

@kenn
Copy link
Owner

kenn commented Feb 19, 2023

PR for fix welcomed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants