0

I'm creating an application where a "submission" can be made using a form which creates client details and allows "referrals" to be created depending on the branch(es) that can provide the required service

class Submission < ActiveRecord::Base
  has_many :referrals, :inverse_of => :submission, dependent: :delete_all
  accepts_nested_attributes_for :referrals, :allow_destroy => true
end

class Referral < ActiveRecord::Base
  belongs_to :submission
end

class Branch < ActiveRecord::Base
  has_many :referrals
end

Submissions controller:

def new
  @submission = Submission.new
  @submission.build_client
  @submission.client.build_address
  @submission.referrals.build
end

def submission_params
  params.require(:submission).permit(:consent, :user_id, client_attributes:
    [:client_id, :first_name,
      address_attributes:
        [:first_line, :second_line,]
    ],
    referrals_attributes:
      [:branch_id]
  )
end

The Submission form:

<%= form_for(@submission) do |f| %>
  <%= f.fields_for :referrals do |referral| %>
    <%= render 'referral_fields', f: referral %>
  <% end %>
<% end %>

_referral_fields.html.erb:

    <% Branch.all.where(referrable: true).each do |branch| %>
       <label>
          <%= check_box_tag 'branch_ids[]', branch.id %>
          <%= branch.name %>
       </label>
    <% end %>

What I want is to have checkboxes for each referrable branch. When a branch is ticked and the submission is created, a referral will be created for that branch. However, when I submit the form, I get a validation error of "Referrals can't be blank". Any idea why this is not working?

Any help is most appreciated

3
  • Try changing <%= check_box_tag 'branch_ids[]', branch.id %> to <%= f.check_box 'branch_id', branch.id %> Commented Feb 10, 2016 at 12:29
  • Hi. Just tried that and still the same error Commented Feb 10, 2016 at 12:31
  • Sorry didnt fully get your changes. I'm getting "undefined method `merge' for 1:Fixnum" Commented Feb 10, 2016 at 12:32

1 Answer 1

3

Use collection_check_boxes.

<% # _referral_fields.html.erb %>
<%= f.collection_check_boxes(:branch_ids, Branch.where(referrable: true), :id, :name) do |b|
  b.label { b.check_box } # wraps check box in label
end %>

You would need to whitelist submission[referrals_attributes][branch_ids] - not branch_id.

def submission_params
  params.require(:submission)
    .permit(
      :consent, 
      :user_id, 
      client_attributes: [
        :client_id, 
        :first_name,
        address_attributes: [
          :first_line, :second_line,
        ]
    ],
    referrals_attributes: [:branch_ids]
  )
end

Edited.

However for this to work you need to setup a relation between Referral and Branch. In this case you could use either a has_and_belongs_to_many (HABTM) or has_many though: (HMT) relationship.

See Choosing Between has_many :through and has_and_belongs_to_many.

class Referral < ActiveRecord::Base
  belongs_to :submission
  has_and_belongs_to_many :branches
end

class Branch < ActiveRecord::Base
  has_and_belongs_to_many :referrals
end

You need to create a join table as well:

rails g migration CreateBranchReferralJoinTable branch referral
Sign up to request clarification or add additional context in comments.

10 Comments

I get "undefined method `branch_ids' for #<Referral:0x007f33d537cbb0>"
Ah, its because you need to create a relation between Referral and Branch.
I already have that. branch has many referrals and referral belongs to branch
Yeah, but that won't work unless referral can have only one branch - and in that case you should not use a multiple checkboxes.
You see in the first case you putting .branch_id on the referrals table - with makes it one-to-many - what you need is a many-to-many relationship.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.