how do I style a ruby select in bootstrap? - html

I want to wrap this line of ruby in a select style in bootstrap but every time I do it breaks and lists all the generated options outside the form.
Here in the code I want to wrap
<%= f.label :user_description_id %>
<%= f.collection_select(:user_description_id, UserDescription.all, :id, :description, include_blank: true) %>

You should be doing somethin linke this:
<%= f.label :user_description_id %>
<%= f.collection_select(:user_description_id, UserDescription.all, :id, :description, {include_blank: true}, {class: "css-class"} ) %>
Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-collection_select

Related

Bootstrap Grid Alignment Issues

So I'm following a tutorial for RoR but I can't seem to get the alignment right.If I copy and paste the code for the tutorial, it will look like
this
Here is the code:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
But if I put col-md-1, then it sort of works, looking like this
Is it possible to still use col-md-6 but have it properly aligned like the second photo? I don't know what else to change other than the numbers.
I am not able to test this right now but you may be able to change your first line of your form to:
<%= form_for #user, :html => {:class => "form-horizontal"} do |f| %>
This should add the html and css need to make the form horizontal:
<form class="form-horizontal">
Some what like this example: https://jsfiddle.net/obp9x1yr/7/
This might be a good post to read for more information: Rails Bootstrap how to format form_for (width grid collapses)

Rails: creating a drop down menu for a form

Would you know how to make a pull down or drop down menu for a form text field?
Currently, the user needs to to type in text "Rating" then submit the form. But ideally, they should select a rating of "Awesome" "Good" or "Meh" from a pull down, rather than be able to enter a custom text. Any advice in the right direction would be appreciated.
Thank you.
_form.html.erb
<%= simple_form_for(#post) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :Duration, required: true, autofocus: true %>
<%= f.input :Rating %>
</div>
<div class="form-actions">
<%= f.button :submit, "Log it!" %>
</div>
<% end %>
Please try to replace the line:
<%= f.input :Rating %>
with:
<%= f.select :Rating, options_for_select(["Awesome", "Good", "Meh"]) %>

Using HTML in A Form

I am trying to create a form that accepts HTML. So that when I type <br> or use divs it understands and populated my content block accordingly. Can anyone point me in the right direction for this?
Here's the form I am using if that helps:
<%= simple_form_for(#daily) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title %>
<%= f.input :content %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Replace <%= f.input :content %> with <%= f.text_area :content %>. That doesn't do much but make a multi-line input area (looks better). However, when you display the data on your webpage (i.e. after you submit the form), you can put something like this:
<p>#daily.content.html_safe</p>

Use SimpleForm to have multiple options in Dropdown

I'm using Ruby on Rails and trying to add make a dropdown form with only specified options, those options being states.
So far I have the following:
<%= simple_form_for #location do |f| %>
<%= f.input :address %>
<%= f.input :state %>
<%= f.button :submit, "Add Location" %>
<% end %>
However, I want to add something like
<%= f.input :state %>
but only allowing all of the two letter abbreviations for all of the states in the USA (AL, AK, AZ, etc.)
Note: I have a location table with address and state as columns
Okey you need this if you want it dinamyic values according your table states
**Table
|states|
|id| |name|
1 ALASKA
2 AK
3 AZ
**In your controller:
#states: State.all
**In your view:
States:
<%= f.select "states",options_for_select(#states.collect {|t| [t.name.to_s ,t.id]}, params[:states].to_i ) %>
And If you want it with static values you should try:
<%= f.input :state, collection: ['AL', 'AK', 'AZ'] %>
Ok, figured it out:
<%= f.input :state, collection: ['AL', 'AK', 'AZ'] %>
and so on...

Basic rails form, does not deselect alternative options - can't find solution for rails

I have excel, video, and multiple choice boolean columns in my Step model. I am trying to get it so the user has to choose one of the three, and when saved it passes back to the database as true. Right now my two problems are a)when I select one, it doesn't deselect the others and b) the radio button is on a different line than the text that labels it. Any help would be appreciated.
<fieldset class="stepCreator">
<%= "Step" %>
<%= f.label :description, "Description" %>
<%= f.text_field :description %>
<div>
<%= f.label :excel, "Excel" %>
<%= f.radio_button(:excel, true, :checked => true) %>
<%= f.label :video, "Video" %>
<%= f.radio_button(:video, true) %>
<%= f.label :multiple_choice, "Multiple Choice" %>
<%= f.radio_button(:multiple_choice, true) %>
</div>
<%= f.hidden_field :_destroy %>
<%= link_to "remove", '#', class: "btn btn-danger btn-mini remove_fields "%>
</fieldset>
Actually you have 2 very different problems.
All the radio buttons (that you want to select one and automatically deselect the others) must have the same name, and different values, so it should be something like:
f.radio_button(:media, :excel, :checked => true)
f.radio_button(:media, :video)
For the labels to be at the same line as the checkbox, you just have to change the CSS.
From the documentation:
http://guides.rubyonrails.org/form_helpers.html
try:
<%= radio_button_tag(:age, "child") %>
<%= label_tag(:age_child, "I am younger than 21") %>
<%= radio_button_tag(:age, "adult") %>
<%= label_tag(:age_adult, "I'm over 21") %>
radio buttons have to select the same attribute in your model, in this example i selected the age attribute and i can give it 1 of two values, or he is a child or he is an adult
hope it helps