Skip to content

Instantly share code, notes, and snippets.

@francois-blanchard
Created April 3, 2015 14:45
Show Gist options
  • Save francois-blanchard/98737a9c5d97b8d1b9d5 to your computer and use it in GitHub Desktop.
Save francois-blanchard/98737a9c5d97b8d1b9d5 to your computer and use it in GitHub Desktop.
Create nested attributes with checkbox

Create nested attributes with checkbox

# Class
has_many :sc_invited_celebrities
accepts_nested_attributes_for :proposed_posts, :sc_invited_celebrities, :answer_to_questions
# Controller
def new
  @social_campaign = SocialCampaign.new
  SocialCampaign.influencers.each {|influencer| @social_campaign.sc_invited_celebrities.build(:celebrity_id => influencer.id)}
end
# View
<%= f.fields_for :sc_invited_celebrities do |invited_celebrity| %>
  <div class="sub-field">
    <%= invited_celebrity.check_box :_destroy, {:checked => invited_celebrity.object.persisted?},'0', '1' %>
    <%= invited_celebrity.label :_destroy, invited_celebrity.object.celebrity.full_name %> <br \>
    <%= invited_celebrity.hidden_field :celebrity_id, value: invited_celebrity.object.celebrity_id %>
  </div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment