Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “simple_form

Simple_Form Custom Errors

I had some error messages not appearing in simple_form because the fields they were related to did not appear in the simple_form.  Normally, simple_form will highlight the field in the form that has a problem and display the error message next to it.  However, since the field related to the error wasn’t in the form, there was no message being shown.

To solve this, I had to add some additional code to display the text of the error message.

I fixed it by adding this to my simple_form forms:

<%= render 'shared/error_messages', object: f.object %>

and then created a partial, _error_messages (shared/_error_messages):

<% if object.errors.any? %>

<% end %>

Advertisements

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 %>


Inline Link for Field in Simple Form

I needed to put a link to the right of a checkbox on my user sign-up form so that people could view my app’s terms of service and privacy policy before agreeing to it.  I was having trouble figuring out how to do it with Simple_Form, but eventually I was able to figure it out based on this Stack Overflow question.

My final code looked like this:

<%= content_for(:the_links) do %>
  I agree to the <%= link_to "Terms of Service", terms_path,:remote => true %>
  and <%=link_to "Privacy Policy", privacy_path, :remote => true%>
<% end %>

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= f.input :terms, :as => :boolean, boolean_style: :inline, :label => content_for(:the_links)%> 
<% end %>