JP Silvashy
JP Silvashy

Reputation: 48475

Rails, for each checkbox in a form, create a model?

I'm confused about how to do this, basically what I have is a Notification model, and users have_many notifications. The issue is, I need to create a form so that it has checkboxes for each user, and if the user is checked, they get the notification.

Here is what I created so far, but I'm thinking the form itself shouldn't be for the notification model?

My controller:

class Admin::NotificationsController < AdminController
  respond_to :html

  def index
  end

  def new
    @notification = Notification.new
    @users = User.all
  end
end

View:

<%= simple_form_for [:admin, @notification] do |f| %>

  <%= f.input :content %>

  <label>create for users:</label>
  <% @users.each do |user| %>
    [checkbox] <%= user.email %>
  <% end %>

  <%= f.button :submit, :class => "primary" %>
<% end %>

I guess I just don't know where to start with the form, any help explaining how would really really be appreciated!

Upvotes: 0

Views: 214

Answers (1)

ka8725
ka8725

Reputation: 2918

Use:

<%= f.association :users, :as => :check_boxes %>

Upvotes: 1

Related Questions