Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “form

Select Drop Down in Simple_Form and Rails

I’m using the simple_form gem for forms in Rails and I needed to create a select (drop down) for a field.  In my case, it was for selecting US States, but this can be changed easily to whatever options you want to present by changing the options in the list.

In my user.rb model, I put the options (change these if your select is for something else than US States).

STATES =
[
[‘Alabama’, ‘AL’],
[‘Alaska’, ‘AK’],
[‘Arizona’, ‘AZ’],
[‘Arkansas’, ‘AR’],
[‘California’, ‘CA’],
[‘Colorado’, ‘CO’],
[‘Connecticut’, ‘CT’],
[‘Delaware’, ‘DE’],
[‘District of Columbia’, ‘DC’],
[‘Florida’, ‘FL’],
[‘Georgia’, ‘GA’],
[‘Hawaii’, ‘HI’],
[‘Idaho’, ‘ID’],
[‘Illinois’, ‘IL’],
[‘Indiana’, ‘IN’],
[‘Iowa’, ‘IA’],
[‘Kansas’, ‘KS’],
[‘Kentucky’, ‘KY’],
[‘Louisiana’, ‘LA’],
[‘Maine’, ‘ME’],
[‘Maryland’, ‘MD’],
[‘Massachusetts’, ‘MA’],
[‘Michigan’, ‘MI’],
[‘Minnesota’, ‘MN’],
[‘Mississippi’, ‘MS’],
[‘Missouri’, ‘MO’],
[‘Montana’, ‘MT’],
[‘Nebraska’, ‘NE’],
[‘Nevada’, ‘NV’],
[‘New Hampshire’, ‘NH’],
[‘New Jersey’, ‘NJ’],
[‘New Mexico’, ‘NM’],
[‘New York’, ‘NY’],
[‘North Carolina’, ‘NC’],
[‘North Dakota’, ‘ND’],
[‘Ohio’, ‘OH’],
[‘Oklahoma’, ‘OK’],
[‘Oregon’, ‘OR’],
[‘Pennsylvania’, ‘PA’],
[‘Puerto Rico’, ‘PR’],
[‘Rhode Island’, ‘RI’],
[‘South Carolina’, ‘SC’],
[‘South Dakota’, ‘SD’],
[‘Tennessee’, ‘TN’],
[‘Texas’, ‘TX’],
[‘Utah’, ‘UT’],
[‘Vermont’, ‘VT’],
[‘Virginia’, ‘VA’],
[‘Washington’, ‘WA’],
[‘West Virginia’, ‘WV’],
[‘Wisconsin’, ‘WI’],
[‘Wyoming’, ‘WY’]
]

Now in my simple_form, I put this:

<%= simple_form_for(@user) do |f| %>
<%= f.input :indiv_state, collection: User::STATES %>
<%= f.button :submit %>
<% end %>


Allowing HTML Tags in Form Fields

I wanted to allow my users to be able to include html tags in things they posted.  Originally, I was thinking that this was done as an option to the form field, but it actually is in the view where you display the user content.

In my case, I have this in the form:

<%= f.text_area :content %>

and where you want to display it, just add “.html_safe”, for example:

<%= article.content.html_safe %>


Select Default

To have a default value above the options in a Rails select box, use :prompt.

ie

<%= f.select(:field_name, {“option1″=>”option1″ ,”option2″ =>”option2″}, :prompt=>”Please select an option”) %>


Default Values and Placeholders in Text Fields

You can place text in fields to pre-populate them or to save space by putting the label in the field with simple html.

If you want label a field, you can use the placeholder tag which works in most modern browsers:

If you want to pre-populate a field, with text, you can use the value tag which will put the text, but NOT remove it when the focus is put on the field:


Aligning Image_Submit_Tag with form elements

To align the image and form element, try:

<%= image_submit_tag “image.jpg”, :style => “position:absolute”  %>

http://tech.gaeatimes.com/index.php/archive/how-to-perfectly-align-image-button-with-html-input-textbox/