0

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!

1 Answer 1

1

Use:

<%= f.association :users, :as => :check_boxes %>
Sign up to request clarification or add additional context in comments.

2 Comments

It's weird, I get Association :users not found, even though all my associations are setup correctly!
Try <%= f.collection_check_boxes :user_ids, @users, :id, :email %> also make sure that you have has_many and belongs_to associations correctly also see if you have notification_id field in User model.

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.