Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My suggestion:</p> <p>Create a user form to set up the associations between <code>User</code> and <code>Sublocation</code></p> <p>I am assuming the sublocations have been pre-populated?</p> <p><strong>users_controller.rb</strong></p> <pre><code>def new @user = User.new @sublocations = Sublocation.all respond_to do |format| # what ever end end def create @user = User.new(params[:user]) params[:sublocation_ids].each do |key, value| @user.affiliations.build(sublocation_id: value) end respond_to do |format| if @user.save # what ever else # what ever end end end def show @user = User.find(params[:id]) @affiliations = @user.affiliations end def set_default affiliation = Affiliation.find(params[:affiliation_id]) Affiliation.where(user_id: params[:user_id]).update_all(default: false) affiliation.toggle!(:default) redirect_to affiliation.user, notice: "Default sublocation set" end </code></pre> <p><strong>users/_form.html.haml</strong></p> <pre><code>= form_for @user do |f| .field # fields for user attributes .field %h1 Sublocations = f.fields_for "sublocations[]" do - for sublocation in @sublocations = label_tag sublocation.name = check_box_tag "sublocation_ids[#{sublocation.name}]", sublocation.id, @user.sublocations.include?(sublocation) .actions = f.submit 'Save' </code></pre> <p>Then perhaps on the user show page, list all their sublocations and create a form for them to set a default.</p> <p><strong>users/show.html.haml</strong></p> <pre><code>= form_tag user_set_default_path(@user), method: :put do %table %tr %th Sublocation %th Default? - @affiliations.each do |affiliation| %tr %td= affiliation.sublocation.name %td= radio_button_tag :affiliation_id, affiliation.id, affiliation.default? ? {checked: true} : nil = submit_tag "Set default" </code></pre> <p><strong>routes.rb</strong></p> <pre><code>resources :users do member do put "set_default" end end </code></pre> <p>Hope this helps or at least gets you on the right path. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload