Hide f.select to users

1

I would like to duplicate _form.html to show different options to each person logged in, for example one person can see from f.select 4 options out of 20 that exist, another person can see 3 options out of 20.

How could I create this? I'm pretty confused how to do this

<% programa = {
 'Cemento/mortero' => 'Cemento/mortero',  'Laminas' => 'Laminas', 'Pintura' =>  'Pintura', 'Tinacos' => 'Tinacos', 'Aspersoras' => 'Aspersoras', 'Calentadores solares' => 'Calentadores solares', 'Baños ecológicos' => 'Baños ecológicos', 'Huertos de traspatio' => 'Huertos de traspatio', 'Mejoramiento de vivienda' => 'Mejoramiento de vivienda', 'Mejoramiento de escuelas' => 'Mejoramiento de escuelas ', 'Calzado escolar' => 'Calzado escolar', 'Ampliación de metas en obra pública y activos fijos.' => 'Ampliación de metas en obra pública y activos fijos.'} %>
<%= f.select :programa,programa, label: "Programa",  control_col: "col-lg-4"%>
    
asked by Angel Ac 16.06.2016 в 00:54
source

2 answers

0

Maybe you can create a table named program, with a field id and a field nombre . Also a user type that defines what programs that user can see. Then you create another called programs_type_user, which serves as a many-to-many connection, with a field usuario_id and programa_id .

In the model of type_users you make the connection with the one of programs:

has_and_belongs_to_many :programas

In the program model you make the connection with the type of users:

has_and_belongs_to_many :tipo_usuarios

In the user model you make the connection with programs:

belongs_to :tipo_usuario has_many :programas, through: :tipo_usuarios

Then you create in seed.rb what programs each type of user has access to, then in the user table you define with a tipo_usuario_id field to which type it belongs.

Finally in the form you can do something like this:

<%= f.select :programa, usuario.programas, label: "Programa", control_col: "col-lg-4"%>

    
answered by 16.06.2016 в 22:27
0

You have many ways of doing it, I believe that with the information you give it is better to go to the simple for now.

What we are going to do is show a select or another depending on the user through a conditional. I'll take your example:

    <% programa = if condicion_1 
         { 'Calzado escolar' => 'Calzado escolar', 'Ampliación de metas en obra pública y activos fijos.' => 'Ampliación de metas en obra pública y activos fijos.'} 
                  elsif condicion_2   
         { 'Cemento/mortero' => 'Cemento/mortero',  'Laminas' => 'Laminas', 'Pintura' =>  'Pintura'} 
       end %> 

    <%= f.select :programa, programa, label: "Programa",  control_col: "col-lg-4"%>

In condicion_1 and condicion_2 you must set the conditions for which you want to show one or another set of options.

If you already want to improve your code a bit more, you may want to do this control in the controller and not in the view, introducing the set of options in another variable, leaving your view a bit cleaner.

You can also even create a model with the options and make a query, but I think this is already very complicated for the scope of the question you have asked. (If you need to expand any of the options please indicate in the comments).

    
answered by 28.07.2016 в 15:53