Look like you have some nested attributes there.  My rule of thumb is that
if the form seems very deadpan simple AND not likely to change very much,
you can try to solve the issue using accepts_nested_attributes_for only (
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).


However, if the form even has even a hint of complexity, or might become
more complex in the future, I'd use form objects  (
https://robots.thoughtbot.com/activemodel-form-objects).  I use these 2
libraries and I can pretty much "fake" almost any form object:

- https://github.com/solnic/virtus
- http://api.rubyonrails.org/classes/ActiveModel/Model.html

You can use form objects to manage the inputs validation and
 parameters-massaging, or whatever else you might want, but I recommend
using a service object to handle the actual submitting of the form.
Whenever I try to force complex things into accepts_nested_attributes_for,
I usually spend a few hours sobbing quietly in the corner.

Good luck!


Ben W

On Mon, Jan 23, 2017 at 11:07 AM Hillary Hueter <[email protected]>
wrote:

I'm Not Sure How To Implement this form.

I'm setting up a Tournament Management app. When a User Creates a
Tournament they pick the contact methods that the tournament will use for
contacting participants. This List is defined by the Values seeded into
Contact Methods. When another User Registers for the Tournament, They Can
Pick From any of the Tournament's Contact Methods and if neccessary provide
optional information.

I'm not really sure how to setup the form but what i want it to look like
this:

<https://lh3.googleusercontent.com/-soN19ipHYyc/WIZTVWtYmZI/AAAAAAAAAXw/LaPPNZm5FfcmGlG1VZ0T-6AOP4hsl4egwCLcB/s1600/Desired%2BForm.PNG>



My Code

*Models*

class ContactMethod < ActiveRecord::Base
    has_and_belongs_to_many :tournaments
    has_and_belongs_to_many :registrations
end


class Tournament < ActiveRecord::Base
  has_and_belongs_to_many :contact_methods
end



class Registration < ActiveRecord::Base
  has_and_belongs_to_many :contact_methods
end


*Schema*
  create_table "contact_methods_registrations", force: :cascade do |t|
    t.integer "contact_method_id"
    t.integer "registration_id"
    t.string  "value"
  end

  create_table "contact_methods_tournaments", force: :cascade do |t|
    t.integer "contact_method_id"
    t.integer "tournament_id"
  end

*Form*

<fieldset class="form-group">
      <legend>Contact Method</legend>
      <%= hidden_field_tag("registration[contact_methods][]", nil) %>
      <% @tournament.contact_methods.order(:name).each do |contact| %>
          <div class="form-check">
            <label class="form-check-label">
              <%= check_box_tag("registration[contact_methods][]",
contact.id, contact.id.in?(@registration.contact_methods.collect(&:id))) %>
             <%= contact.name %>
            </label>
          </div>
          <div>
            <label>Your <%= contact.name %> Username</label>
            <%= text_field_tag "registration[contact_methods][]" %>
          </div>

        <% end %>
    </fieldset>


Thanks.

-Hillary


-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
---
You received this message because you are subscribed to the Google Groups
"SD Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
For more options, visit https://groups.google.com/d/optout.

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to