Simple_form disabling selection item according to registration status

1

I have f.association that brings all registered clients. On the client I have a status field.

I would like that when the status is false , the client appears in the select box, but it is not selectable, is this possible?

I know how to use disabled in HTML, what I need to know is whether I can do this with simple_form in rails. For example: f.association, algum_parametro => false .

My code that generates this select is as simple as possible:

simple_form_for(@packing_list) do |f| f.association :client

    
asked by anonymous 06.10.2014 / 18:10

1 answer

1

The documentation in GitHub contains an example of integrating simple_form with a helper of Rails 3, which then allows you to pass an hash of additional attributes for each option.

See:

<%= f.input :role do %>
  <%= f.select :role, 
               Role.all.map { |r| [r.name, r.id, { class: r.company.id }] },
               include_blank: true %>
<% end %>

In the example, a class attribute is set. A% conditional% could be done as follows;

<%= f.input :role do %>
  <%= f.select :role, 
               Role.all.map { |r| [r.name, r.id, { 
                   class: r.company.id, 
                   disabled: ('disabled' if r.status) }] 
               }.reject{ |k,v| v.nil? },
               include_blank: true %>
<% end %>

The above excerpt creates a hash containing disabled as disabled='disabled' is true or status when disabled=nil is false. Then, status removes null attributes, so only the presence of reject will disable the item. This hash idea was based on a SO .

Honestly, the documentation is a bit scarce, so I do not know if it's the right way.

    
07.10.2014 / 14:35