-1

I am trying to display checkboxes on a form that show all of the contacts for a user, so that the user can then select the contacts required. I have done this on the form so far, which displays the checkboxes but with no value:

    <% @contacts.each do |contact| %>
        <% if contact.user.id == User.find(session[:user_id]).id %>
            <%= f.check_box :to %>
        <% end %>
    <% end %>

But when I try to change the line <%= f.check_box :to %> to <%= f.check_box :to, contact.email %> to show the email address of the contact I get the error:

undefined method `merge' for "email@123.com":String

Is there a way around this? I have looked at multiple posts before and tried various options but none seem to help, including:

Ruby on rails f.label for a check box http://apidock.com/rails/v4.0.2/ActionView/Helpers/FormOptionsHelper/collection_check_boxes How to add a checkbox for each row in Rails 3.2 index page?

Community
  • 1
  • 1
Ben Smith
  • 809
  • 5
  • 21
  • 46

1 Answers1

1

You have too much logic in the view. I can't see your code, but I can give you some recommendations:

In the controller:

@contacts_of_current_user = @contacts.where(user_id: session[:user_id])

View:

<% @contacts_of_current_user.each do |contact| %>
      <%= f.label :to, contact.email %>
          <%= f.check_box :to, {}, contact.email %>
      <% end %>
<% end %>

Edit

From the comments I understood that you need to get a string of chosen emails.

There are two ways you can do it.

1) Somehow build this string on the client side (JavaScript)

2) Or pass an array of emails and then concatenate them into single string on the server

I can show you how you can pass an array:

<%= f.collection_check_boxes(:to, @contacts, :email, :email ) %>

Or you can do it manually, something like this:

  <% @contacts_of_current_user.each do |contact| %>
      <%= f.label :to, contact.email %>
          <%= check_box_tag 'to[]', contact.email %>
      <% end %>
<% end %>
chumakoff
  • 6,807
  • 2
  • 23
  • 45
  • Thanks for your suggestion but that still doesn't work – Ben Smith Aug 02 '16 at 14:11
  • What do you need to pass in the `contact[to]` parameter? – chumakoff Aug 02 '16 at 14:14
  • I need to pass the email address, hence why I am trying contact.email in the above – Ben Smith Aug 02 '16 at 14:17
  • Do you want to get an array of emails? Or a few emails as one string? – chumakoff Aug 02 '16 at 14:20
  • kind of, what my aim is is for a user to select say 4 email addresses and then these email addresses get concatenated into one and saved in the database, e.g. "1@a.com;2@b.com;3@c.com;4@d.com" – Ben Smith Aug 02 '16 at 14:22
  • In time I will plan on splitting these out, so one record for "1@a.com", then another for "2@b.com", etc. – Ben Smith Aug 02 '16 at 14:23
  • Again, thanks, but `<%= f.collection_check_boxes(:to, @contacts, :email, :email ) %>` shows the email addresses but when one is ticked and I click "submit" it is not saved in the database - for the record the email address field is blank – Ben Smith Aug 02 '16 at 14:37
  • I need more code to understand what you need. You can easily do it by yourself. See in the log what parameters you get and what is wrong. I gave you base recommendations. – chumakoff Aug 02 '16 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118924/discussion-between-ben-smith-and-chumakoff). – Ben Smith Aug 02 '16 at 14:40