Simple_form disabling selection item according to registration situation

Asked

Viewed 84 times

1

I have a f.association that brings all registered customers. In the client I have a field status.

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

I know how to use the disabled in HTML, what I need to know is if it is possible to 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

  • Adell, welcome to [en.so]! You could edit the question and post the snippet of your screen that generates the select?

1 answer

1


To documentation on Github contains an example of simple_form with a helper of Rails 3, which then allows you to pass a hash of additional attributes for each option.

Behold:

<%= 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, an attribute class is defined. A disabled 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 section creates a hash containing disabled='disabled' how much status is true or disabled=nil when status is false. Then, the reject removes the null attributes, necessary because only the presence of the disabled would disable the item. This idea of hash was based on a issue of the OS.

Quite frankly, the documentation is somewhat scarce, so I do not know if it is the most appropriate form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.