Style Rails Select Tag - ruby-on-rails

I have a form in rails with a select tag. I'm trying to style it like so:
<%= f.select :role, collection: User.roles.keys.to_a, class:"form-control form-control-lg" %>
I have tried the following:
<%= f.select :role, collection: User.roles.keys.to_a, {}, class:"form-control form-control-lg" %>
and
<%= f.select (:role, collection: User.roles.keys.to_a, class:"form-control form-control-lg") %>
But can't get the style to apply to the drop down.

Can you try the following
<%= f.select :role, options_for_select(User.roles.keys.to_a, params[:role]), {}, class: 'form-control form-control-lg' %>
I think will help

Related

How do I add an autocomplete="street-address" attribute to rails form_for

I'm trying to add Mapbox autofill to my address bar on rails, for mapbox autofill to work I need to have autocomplete="street-address in an input field.
I'm using simple_form_form and I can't figure out how to add autocomplete attribute. Here is my form:
<%= simple_form_for #lock do |f| %>
<div class="form_wrapper">
<div class="form_div">
<%= f.input :name %>
<%= f.input :description %>
<mapbox-address-autofill>
<%= f.input :address, :autocomplete => "street-address" %>
</mapbox-address-autofill>
<%= f.input :photo, as: :file, label: "Link to picture" %>
<%= f.input :special_content %>
<%= f.button :submit, :class => "btn btn-primary" %>
<% end %>
any ideas on how to make the autocomplete attribute work?
Use input_html and pass the additional options like class etc.
<%= f.input :address, input_html: { :autocomplete => "street-address"} %>

PG::DatatypeMismatch: ERROR: argument of WHERE must be type boolean, not type integer

I am getting this error in the Production site and this is the code in my views
<%= form_for [:admin, #course], :remote => true do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :duration %>
<%= f.number_field :duration, class: "input-md form-control mb-20"%>
<%= f.label :program_id %>
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
<%end%>
This works in my local server where i have sql db setup.
Program model
has_many :courses
can anyone guide me?
Where clause isn't calling anything to compare with, so PG doesn't know what to include in the results. A where clause must evaluate to true/false.
just replace
<%= f.collection_select :program_id, Program.where('id'), :id, :name, {}, {class: "input-md form-control mb-20" } %>
by
<%= f.collection_select :program_id, Program.all, :id, :name, {}, {class: "input-md form-control mb-20" } %>
If you have some problem with some of the Program in your data base, add a column as status in programs table and make changes here as
<%= f.collection_select :program_id, Program.where("status =?", true), :id, :name, {}, {class: "input-md form-control mb-20" } %>

Collection_Select Does Not Respond to Input_HTML: {Multiple: True}

I am attempting to create a feature where users can add an existing record, recipe, to a collection of records, menu. I am using collection_select with a simple_form to allow users to select multiple records from a list however, the form is not responding to the input_html: { multiple: true } option, which should allow users to select multiple values. The form is as below, please let me know if any other code would be helpful for context.
Form:
<%= simple_form_for #menu, local: true do |f| %>
<%= f.label :title, :class => "form-component-header" %>
<%= f.text_field :title, :class => "form-field" %>
<%= f.label :recipe_ids %>
<%= f.collection_select :recipe_ids, f.object.user.recipes, :id, :title, input_html: { multiple: true } %>
<%= f.submit :class => "form_button" %>
<% end %>
.permit(....recipe_ids: [])
You need to update the permitted parameters in your controller. Now that you are sending multiple selections the parameter needs to be marked as expecting an array.

Simple_form and association

I have this form:
<%= simple_form_for #request, html: {class: 'form-horizontal' } do |f| %>
<%= f.input :initiator, label: 'initiator' %>
<%= f.association :department, collection: Department.all, value_method: :id, label: 'Назначить на отдел' %>
<%= f.association :user, collection: User.all, label_method: :email, label: 'Ответственный' %>
<%= f.input :comment, label: 'comment' %>
<%= f.input :sla, label: 'SLA' %>
<%= f.button :submit, label: 'Создать', class: "btn btn-primary" %>
<% end %>
How can I make the association:
If I choose "Department 1" from: Department, the choice of the :user will only users who belong to this department. (When you open the drop-down list were only people from the Department 1, not Users.all) What parameters I have to pass the rails?
I doubt if it is possible only using simple_form
Try http://www.petermac.com/rails-3-jquery-and-multi-select-dependencies/
I would advice against loading all users in DOM, use an ajax action to filter users if they are many

How do I enable multiple selection for option_form_collection?

I can't figure out how to enable multiple selection for a input in Ransack.
<%= search_form_for #q,url: search_table_path do |f| %>
<%= f.label :country_code_eq %>
<%= f.select :country_code_eq,
options_from_collection_for_select(Country.all, :code, :name),
{prompt: 'Select a Country',multiple: true,include_blank: false}%>
<%= f.label :date_start %>
<%= f.text_field :date_start %>
<%= f.submit %>
<% end %>
The multiple: true does not work as I expected. It only show a normal dropdown box not a multiple selection box.
My first question is how to enable multiple selection ?
And my Second question is how do I keep the selected value and show it after the page loaded in selection box ?
I found the answer
<%= f.select :country_code_in, Country.all.map {|country| [country.name,country.code] }, {include_blank: 'All'}, {multiple: true} %>

Resources