Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using :checked with collection_check_boxes, existing associations should be ignored #341

Closed
watson opened this issue Oct 3, 2011 · 3 comments
Labels

Comments

@watson
Copy link

watson commented Oct 3, 2011

Consider a User who can have many Roles:

class User < ActiveRecord::Base
  has_many :user_roles
  has_many :roles, :through => :user_roles
end

class UserRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end

class Role < ActiveRecord::Base
  has_many :user_roles
  has_many :users, :through => :user_roles
end

One can make a view where a user can be edited and roles can be selected:

= simple_form_for @user do |f|
  = f.name
  = f.email
  = f.association :roles, :as => :check_boxes

By default, when requesting the edit form above, simple_form will list all possible roles that a user can have, checking the onces where a UserRole record exists for the @user object.

But if I add the :checked => [...] option to the f.association call with an array of role id's, I would expect simple_form to pre-check those roles and those roles only. What I get instead, is a set of check boxes with both the existing roles checked + the roles given in the :checked array (i.e. it has merged the @user.user_roles array with the :checked array).

I see the same issue when using f.collection_check_boxes :role_ids, Role.all, :id, :name, :checked => [...] instead of f.association ....

I would expect the array given to :checked to override any existing associations on the @user object. Otherwise simple_form will re-check un-checked check boxes on form validation errors.

rafaelfranca added a commit that referenced this issue Oct 5, 2011
@rafaelfranca
Copy link
Collaborator

I tried to reproduce this issue and I saw that this is the default behavior of check_box rails helper. I have not figured out how to fix this issue. Any clues @carlosantoniodasilva?

@carlosantoniodasilva
Copy link
Member

Hey guys, sorry to delay that much to answer you here.

It really seems to be more a Rails issue than a SimpleForm one, we will try to check what Rails and SimpleForm are doing to see if we can find out what's going on here. Thanks.

nashby pushed a commit to nashby/simple_form that referenced this issue Dec 2, 2011
@rafaelfranca
Copy link
Collaborator

Closed by #374

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants