Allow rails-bootstrap-form collection to accept enumerize values - ruby-on-rails

I am trying to drop in enum values setup with the 'Enumerize' gem into the radio collection select field on a rails-bootstrap-form.
An example is shown how to do this with an ActiveRecord collection. How should I modify the example
<%= f.collection_radio_buttons :skill_level, Skill.all, :id, :name %>
So that it can accept an enum that is setup on my model
<%= f.collection_radio_buttons :level %>
skill.rb
class Skill < ActiveRecord::Base
extend Enumerize
enumerize :level, in: [:good, :ok, :bad ]
end
The Enumerize documentation says
SimpleForm
If you are using SimpleForm gem you don't need to specify input type
(:select by default) and collection:
<%= simple_form_for #user do |f| %>
<%= f.input :sex %>
<% end %>
and if you want it as radio buttons:
<%= simple_form_for #user do |f| %>
<%= f.input :sex, :as => :radio_buttons %>
<% end %>

If you're using simple-form, then the documentation says all you need to do is this:
<%= simple_form_for #skill do |f| %>
<%= f.input :level %>
<% end %>
If you're using rails-bootstrap-form, the documentation shows how to use collection_radio_buttons with Rails's built-in enum functionality, which you aren't using. So I believe you would need to pass in the Skill.level.options call as the collection values, but the :level method for (as the method, value_method, and text_method arguments):
<%= bootstrap_form_for #skill do |f| %>
<%= f.collection_radio_buttons :level, Skill.level.options, :level, :level %>
<% end %>

Related

HABTM association dropdown select

I am trying to create a dropdown select on a form. I have a HABTM association between professors and classrooms:
Classroom Model:
class Classroom < ApplicationRecord
has_and_belongs_to_many :professors
end
Professor Model:
class Professor < ApplicationRecord
has_and_belongs_to_many :classrooms
end
Strong Params:
def classroom_params
params.require(:classroom).permit(:name, :professor_ids => [])
end
I am trying to find a way to use f.select instead of select_tag inside the form. But when I do it, the database does not save the values. This way works:
<%= form_for #classroom do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<% array = Professor.all.map { |professor| [professor.user.name, professor.id] } %>
<%= select_tag "classroom[professor_ids][]", options_for_select(array) %>
<% end %>
But I am trying like that and it is not working:
<%= form_for #classroom do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<% array = Professor.all.map { |professor| [professor.user.name, professor.id] } %>
<%= f.select :professor_ids, options_for_select(array) %>
<% end %>
The view works correctly but when I submit the form, the value doesn't go to to the classroom_params. I tried to debug it stopping the controller after the submit and I got this:
The params came correctly with all the information submitted, but the classroom_params came missing the professor_ids.
Is there a way to do this dropdown using f.select?
You whitelisted the array of 'professor_ids', but your 'select' input returns 1 string ("prefessor_ids" => "2" from you screenshot). Maybe you want to set the select as 'multiple'? (I have not tested it, but i think params will be whitelisted correctly after that)
<%= form_for #classroom do |f| %>
<%= f.label :name %><br>
<%= f.text_field :name %><br>
<%= s.collection_select :professor_ids, Professor.all, :id, :name, multiple: true %>
<% end %>
where
class Professor
...
delegate :name, to: :user
end
Update
You probably don't have 'cfg' variable in your controller action.

Setup Rails Form Builder for Related Resource

I do not understand how to setup forms with related resources in Rails 4.
My models:
class Task < ActiveRecord::Base
belongs_to :category
accepts_nested_attributes_for :category
end
class Category < ActiveRecord::Base
end
I'm trying to setup a form where I can simply select the "category" from a drop down box.
How do I setup my controller and form view to accomplish this?
Try this:
<%= form_for #task do |f| %>
<%= f.collection_select :category_id, Category.all, :id, :name, {}, { :multiple => false } %>
<% end %>
This assumes you have an attribute in your Category model called name.
Add category_id to your strong parameters in your Tasks Controller.
An example with and without simple_form
<%= simple_form_for #object do |f| %>
<%= f.input :name %>
<%= f.association :company %>
<%= f.button :submit %>
<% end %>
And without sf
<%= form_for #object do |f| %>
<%= f.collection_select :company_id, Company.all, :id, :name =>
<% end %>
Both will produce a dropdown of company's using the name in the select list.

Rails checkboxes

I have a string column in my database which is either "artist" or "listener", and I want the user to be able to choose which string the column is populated with by clicking the appropriate checkbox. How would I do this?
You should use radio button here:
# Imagine it is User model and user_type field
<%= form_for User.new do |f| %>
<%= f.radio_button :user_type, "artist" %>
<%= f.radio_button :user_type, "listener" %>
<% end %>
f.check_box :my_field, {}, "artist", "listener"
This would make my_field be "artist" when it's checked, and "listener" when unchecked.
You should use radio-buttons for that matter. Also make sure to put that logic into the model (validations).
# model
class User
TYPES = %w(artist listener)
validates_inclusion_of :user_type, :in => TYPES
end
# view
<%= form_for :user do |f| %>
<% User::TYPES.each do |type| %>
<%= f.radio_button :user_type, type %>
<% end %>
<% end %>

Formtastic Confused on Has One Relationships

I'm a bit stuck on a 'has_one' and 'belongs_to' relationship and getting it to properly display in Formtastic. I have a person model that has one picture (a profile picture). I want the user to be able to select the picture using radio buttons. So far, I have:
<% form.inputs do %>
<%= form.input :picture, :as => :radio, :collection => #pictures %>
<% end %>
However, this fails (because the foreign key is stored on the 'belongs_to' side of associations in Rails. Any suggestions?
Ended up using custom controller code to fix. Use a variety of filters, etc.
Came across this in the "related" sidebar. I think this is a good use case for nested attributes -- from the Formtastic README:
Nested forms are also supported (don’t forget your models need to be setup correctly
with accepts_nested_attributes_for). You can do it in the Rails way:
<%= semantic_form_for #post do |form| %>
<%= form.inputs :title, :body, :created_at %>
<%= form.semantic_fields_for :author do |author| %>
<%= author.inputs :first_name, :last_name, :name => "Author" %>
<% end %>
<%= form.buttons %>
<% end %>
Or the Formtastic way with the :for option:
<%= semantic_form_for #post do |form| %>
<%= form.inputs :title, :body, :created_at %>
<%= form.inputs :first_name, :last_name, :for => :author, :name => "Author" %>
<%= form.buttons %>
<% end %>

Ruby on Rails: Drop down menu

I'm trying to create a drop down menu to allow a user to change an entry's field in my table. The user has one of three options -- hot, medium and cold.
I already have text_fields that do essentially the same thing for other fields, that all update when the user clicks on a submit_tag.
Is there an easy way to implement a drop-down box and have the result saved with the submit_tag ?
thanks,
-Chris
Here's the basic answer. The array of two element arrays is the critical part.
<% form_for #entry do |f| %>
<%= f.text_field :name %>
<%= f.select :temperature, [['Hot','hot'],['Medium','medium'],['Cold','cold']] %>
<%= f.submit %>
<% end %>
I'll assume 2 things:
That you are the <%= form_for #model_instance idiom (explained on section 2.2 of this guide).
That you want to store the "hot", "medium" and "cold" values as strings (not as numbers 1,2 and 3 or something similar) on your database.
Let's say that you have two fields, called :name and :temperature, controlled by two text_fields:
<% form_for #article do |f| %>
<%= f.text_field :name %>
<%= f.text_field :temperature %>
<%= f.submit "Create" %> <% end %>
<% end %>
Now you want to change the :temperature control to a dropdown list, accepting hot, medium and cold as values. Then you can do that this way:
<% form_for #article do |f| %>
<%= f.text_field :name %>
<%= f.collection_select :temperature, Article::TEMPERATURES, :to_s, :to_s,
:include_blank => true
%>
<%= f.submit "Create" %> <% end %>
<% end %>
You will now have to define the Article::TEMPERATURES constant in your Article model. It shouldn't be very difficult:
class Article < Activerecord::Base
TEMPERATURES = ['hot', 'medium', 'cold']
You may be wondering why I added the :include_blank part on the collection_select. This will add an "empty" option on your dropdown list. You will need that empty option when creating new objects, unless you want a "default" value to temperature.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M001730
I was working on something similar. I got this to work by simply adding either an enum or a constant(similar to what kikito said previously) in my model and then calling the select in my form.
Here's how it can work.
using the constant:
class ClassName < ActiveRecord::Base
TEMPERATURES = ['Hot', 'Medium', 'Cold']
end
bin/rails g migration add_column_to_table temperatures:string
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName::TEMPERATURE %>
or
using the enum:
class ClassName < ActiveRecord::Base
enum temperature: [:hot, :medium, :cold]
end
bin/rails g migration add_column_to_table temperatures:integer
_form.html.erb
<%= f.label :temperature %>
<%= f.select :temperature, ClassName.temperatures.keys %>
Hope that helps you!
You might want to consider formtastic gem which is lot less code.
<% semantic_form_for #stuff do |f| %>
<% f.inputs do %>
<%= f.input :name %>
<%= f.input :temperature, :as => :select,
:label => "Degree", :include_blank => false,
:collection => [["Hot", 1], ["Medium", 2], ["Cold", 3]] %>
<% end %>
<%= f.buttons %>
<% end %>
In accordance with all of the above answers, remember to do this last, important step:
Restart your server!
As a newbie, I was wondering why my array was not working even though I followed all the steps correctly.

Resources