lundi 23 juillet 2018

Ruby on Rails: Check_boxes for each item in a list

I'm trying to render simple check_boxes in a list of results so users can select on which of these results to get additional content for.

Example model setup (names changed):

UserRequest has_one :response
Response has_many :individual_results
IndividualResult has_one :contact_info

In the UserRequestsController I have a show action, which shows the user_request (duh), response, and its individual_results in a table.

The user should be able to select items (= contact_info) to request additional content for those. For some reason I don't manage to render the check_boxes after days of trying. Basically, I don't really understand where and how to store the array of selected contact_infos and how to pass it to the method that then gets the additional content.

My attempt was:

  • Create a ContactInfosController (didn't exist before, the user was only creating and showing her requests up to now, so contact_info was "model only" before)
  • Create a method request_content(contact_infos) (as post, to pass the user-selected contact_infos to. If I were to make it Restful, it would probably be "edit/update")
  • Add the form with check_boxes search_request#show

My search_request/show.html.erb:

<tbody>
  <% if @response %>
    <% form_for @contact_infos, url: contact_infos_request_content_path(@contact_infos) do |form| %>
      <% @response.individual_results.each do |result| %>
        # result.foo, ...
        # result.contact_info.bar
        <%= form.check_box "contact_info", "contact_info.request_content?", "true", "false %>
      <% end %>
      # form.submit
    <% end %>
  <% end %>
</tbody>

As it didn't work, I also had to declare the instance variable @contact_infos in RequestsController#show as @contact_infos = @response.individual_results.map { |r| r.contact_info }

Now, it fails at the check_box with "undefined method `merge'". Also, not sure how the params would be passed? I feel I went seriously "off the rails" and probably screwed up the design with this as it seems way too complicated...

Would anybody be so kind and help me get into the right direction, e.g., how would you pass the response to a method to request additional information? Read tons online but somehow couldn't apply it.

Thanks so much!




Aucun commentaire:

Enregistrer un commentaire