Custom id for input using simple form in Rails

Simple_form  provides a lot of handy html tags for forms. However, the autocreation of classes and id’s was giving me trouble because I couldn’t figure out how to give an element the id I wanted. It’s actually very simple.

input_html: { id: 'my_id'}

is used to give an id to the specific element. This can be a bit tricky when using collection_select, as it takes a bunch of inputs. I typically put the input_html: {} as the last input.

<%= f.input :item_id, collection: Supplier.order(:name), as: :grouped_select, group_method: :items, group_label_method: :name, label_method: :name, value_method: :id, :prompt => 'Choose an existing Item', input_html: { id: 'quantity_item_select'} %>

Check the documentation for collection_select and grouped_collection_select to be sure you put the inputs in the correct order.

You can also use wrapper_html to input custom html for the wrapper div’s that simple_form creates.

wrapper_html: { id: 'my_id'}

I hope this is useful for you, as even though its simple, it took me a while of searching to find it.

Advertisements

Customize Devise Flash Message Content in Rails

Devise automatically includes great flash messages, however sometimes you want to edit or make your own.  In my case I have customized the devise controller for registrations, and I want to call a flash message in my custom devise registrations controller. (refer to my post on how to override and create a custom devise controller here).

So by looking in the devise source code, in particular the registrations_controller.rb, we see how to do this. First, in the devise.en.yml file, create your custom flash message content in the right location. In my case I want it to be in the registrations controller, so in the devise.en.yml under registrations I created a new flash message like this:

registrations:
  wrong_beta_password: "You need the beta password to create an account." # this one is my custom one
  # Devise default flash messages here

Now, simply call this message in your custom devise controller:

set_flash_message :notice, :wrong_beta_password if is_flashing_format?

That’s it!

Formatting Date and Datetime in Rails

It’s actually pretty easy, and even though the documentation references only show it with datetime, it also seems to work on date objects as well.

So here are the different formats:

 datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0)   # => Tue, 04 Dec 2007 00:00:00 +0000

  datetime.to_formatted_s(:db)            # => "2007-12-04 00:00:00"
  datetime.to_s(:db)                      # => "2007-12-04 00:00:00"
  datetime.to_s(:number)                  # => "20071204000000"
  datetime.to_formatted_s(:short)         # => "04 Dec 00:00"
  datetime.to_formatted_s(:long)          # => "December 04, 2007 00:00"
  datetime.to_formatted_s(:long_ordinal)  # => "December 4th, 2007 00:00"
  datetime.to_formatted_s(:rfc822)        # => "Tue, 04 Dec 2007 00:00:00 +0000"

I used it on a date object with the :long format, and worked like a charm!

credit here