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

Check box collection inputs not checked on collection defined as array of symbols #828

Closed
mszyndel opened this issue Jun 24, 2013 · 16 comments

Comments

@mszyndel
Copy link

Let's say I have a following model

class Palette
  attr_accessible :colours
  serialize :colours, Array
end

and a form

<%= simple_form_for :palette do |f| %>
  <%= a.input :colours, collection: [:red, :green, :blue], as: :check_boxes %>
<% end %>

so that labels get translated automatically when rendering.
The problem I'm facing is that respective check boxes are not not checked when I am editing an object.

I've found out that a reason for that is Simple Form is not matching symbol named collection elements from field to string values retrieved from database.

Workaround for that is to alter the line like this

  <%= a.input :colours, collection: [:red, :green, :blue], as: :check_boxese, value_method: lambda {|v| v.last.to_s } %>

but this is, as you may see, quite annoying.
Is it something that you may try to fix? I think value lookup should be performed either on strings or on symbols. Changing behaviour may break some peoples implementations, so I'm not sure what is the best way to fix it.

@nashby
Copy link
Collaborator

nashby commented Jun 29, 2013

@hajder what Rails and SimpleForm versions are you using? Just tested it with SimpleForm 3.0.0.rc and Rails 4 and looks like everything works as expected. A sample app that reproduces this issue would be really helpful though. Thanks!

@nashby nashby closed this as completed Jul 9, 2013
@mszyndel
Copy link
Author

mszyndel commented Jul 9, 2013

@nashby Hey, excuse my late resp.

simple_form (2.1.0)
rails (3.2.13)

I will make and link test app tomorrow.

@nashby nashby reopened this Jul 9, 2013
@nashby
Copy link
Collaborator

nashby commented Jul 9, 2013

@hajder tried it with these versions but with no success :(
Anyway waiting for the test app. Thanks!

@mszyndel
Copy link
Author

I made a test app but can't reproduce this.
I'll have to investigate into my real app there I used aforementioned fix.

@didieruzan
Copy link

Reproduced the problem (with those same versions mentioned above).
I had the following configuration as well
:label_method => :last,
:value_method => :first,
(as I was using an array of id/value collection).
I then noticed it was properly working (checking the check_boxes for the selected values) when I omitted the :value_method => :first and only left :label_method => :last.

Hope this helps anyone

@yorkxin
Copy link

yorkxin commented Nov 18, 2013

I can reproduce this issue:

  1. serialize :colours, Array
  2. Make a new Palette in rails console, that assigns palette.colours = [:red, :green] (instead of array of strings)
  3. Open palette/edit page

Expected result:

  • red and green are checked.

Actual result:

  • red and green are not checked. All check boxes are not checked.

If string is assigned instead, e.g. palette.colours = ["red", "green"], then it works as expected.

@yorkxin
Copy link

yorkxin commented Nov 18, 2013

I've debugged Checkbox#checked? method in Rails and found that the @checked_value is string but values is Array of Symbols.

So my workaround is:

<%= f.input :colours,
            :as => :check_boxes,
            :collection => [:red, :green, :blue],
            :value_method => :to_sym # convert values to symbol
            %>

@nashby nashby reopened this Nov 18, 2013
@carlosantoniodasilva
Copy link
Member

Just a reminder that whatever is being sent from the form is going to be string, so you're going to receive an array of strings in your controller/model, and you shouldn't be converting them back to symbols because you might be opening a big hole to a symbol DDoS in your application. Thus I'm not sure we should try to make this happen. Thanks!

@josevalim
Copy link
Contributor

I agree with @carlosantoniodasilva. There is no way we can make this reliably work. You need to check the strings are valid values in the model/controller side and convert them to symbols.

@mszyndel
Copy link
Author

hmm. So the lesson learned is not to use symbols in here?

@yorkxin
Copy link

yorkxin commented Nov 18, 2013

Make sense. I'll consider to work on Strings instead of Symbols. Thanks @carlosantoniodasilva :)

@nashby
Copy link
Collaborator

nashby commented Nov 18, 2013

Ok, closing then again. :)

@nashby nashby closed this as completed Nov 18, 2013
@carlosantoniodasilva
Copy link
Member

Thanks everyone 💚💛❤️💙💜

@thadeu
Copy link

thadeu commented Oct 21, 2015

serialize :colours, Array
Resolveu meu problema, acontece que o simple_form não aceitava Array, nem .join()

@mszyndel
Copy link
Author

Hi @thadeu thanks for your comment! Could you please edit it to be in English?

@martinverdejo
Copy link

martinverdejo commented Oct 20, 2016

does anyone have an elegant way disabling inline labels? for example when I'm using the checkbox inside a <td> and will use the label in another <td>.

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

No branches or pull requests

8 participants