Simple Form - Translating options of a input field - ruby-on-rails

I'm trying to use the i18n feature of simple form, which works great in most cases.
My only problem is, that in one case I want to use numbers as option values, so I can not simply create a symbol like in the other cases. Right now, I'm using this solution:
f.input :adm, :as => :select, :collection => [[:adm11 ,"11"],
[:adm00, "00"], [:adm06, "06"], [:adm99, "99"]]
Can I somehow make simple_form look up adm11 and so on in the usual way, so I can keep a sensible structure in my translation file?
I know I could do it with standard ruby i18n, but I'm looking for a better way.

f.input :adm,
:collection => [[:adm11 ,"11"], [:adm00, "00"], [:adm06, "06"],
[:adm99, "99"]],
:label_method => lambda { |el| t "define.i18n.keys.here.#{el.first}" }

I think you can't do it because of this line in SimpleForm:
collection_translated = translate_collection if collection_classes == [Symbol]
So it means that SimpleForm translates options if it's array of symbols. See discussion here https://github.com/plataformatec/simple_form/pull/302

Related

Suppress square brackets in input names generated by simple_form

I'm working on a bookmarkable search form - with simple_form in order to get access to some custom inputs. Since no real model object is connected, I use the :q symbol to fake one:
= simple_form_for :q, url: projects_path, method: :get do |f|
= f.input :area_id,
as: :select,
collection: (...)
= f.input :description,
as: :geocomplete
While this works, the naming conventions produce in not so nice URLs such as:
...?q[area_id]=16&q[description]=Paris&q[lng]=4.123&q[lat]=30.123
Is there a way to tell simple_form to suppress the fake :q object and produce URLs like:
...?area_id=16&description=Paris&lng=4.123&lat=30.123
Thanks for your hints!
Like #Martin wrote, it can't be done. So it's either standard form helpers or accept the square brackets. I've chosen the latter.

How do I generate a bunch of checkboxes from a collection?

I have two models User and Category that have a HABTM association.
I would like to generate checkboxes from a collection of Category items on my view, and have them associated with the current_user.
How do I do that?
Thanks.
P.S. I know that I can do the equivalent for a dropdown menu with options_from_collection_for_select. I also know that Rails has a checkbox_tag helper. But not quite sure how to do both of them. I know I can just do it manually with an each loop or something, but am wondering if there is something native to Rails 3 that I am missing.
Did you check out formtastic or simple_form
They have helpers to write your forms more easily, also to handle simple associations.
E.g. in simple_form you can just write
= simple_form_for #user do
= f.association :categories, :as => :check_boxes
In form_tastic you would write
= simple_form_for #user do
= f.input :categories, :as => :check_boxes
Hope this helps.
You can use a collection_select and feed it the options. Assuming you have a form builder wrapped around a user instance, you can do something like this:
form_for current_user do |f|
f.collection_select(
:category_ids, # the param key, so params[:user][:category_ids]
f.object.categories, # the collection of items in the list
:id, # option value
:name # option string
)
end
You may want to pass the :multiple => true option on the end if needed.

How to pass an array in a variable for :collection in form for rails?

Here is a simple_form code in _form.html.erb.
<%= f.input :start_time, :label => "Start Timeļ¼š", :collection => #time_slot %>
#time_slot is a variable defined in the controller. It is an array and looks like:
#time_slot = ['00:30 AM','01:00 AM','01:30 AM','02:00 AM','02:30 AM','03:00 AM' ,'03:30 AM','04:00 AM','04:30 AM']
The problem is that the rendered view does not have the dropdown menu with the predefined time slots listed. Instead it only shows a text box.
How to show the drop down time slots instead of a text box? Thanks.
Firstly: input was deprecated in v2.3.8, so if you're using Rails3 you should probably not be using input. Instead, use the proper form helpers, for a drop-down box (or select tag) you'd want:
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select
but if you must - you can probably pass the input-type eg :type => :select but I've never tried that and it may not work.

select(object,...) how to get the html options for this type of drop down menu

I'm using this drop down menu for my associations:
<%= select("price", "product_id", Product.all.collect {|p| [ p.name, p.id ] }, {},{ :class=>'chzn-select'}) %>
I was able to get he :class but how do i get other options such as :placeholder and :size?
Help would be highly appreciative, i cant find examples using select only after research.
P.S. Is there a better way to handle this? I am trying to make it more human friendly.
By placeholder do you mean prompt? In that case you'd put it in that first, empty hash, the 3rd argument, for helper options. The fourth is for any and all html tag options.
Edit: By placeholder do you mean HTML5's placeholder? Does that even apply to <select> tags? In any case, the same options hash where you specified the class would the place for any valid HTML options.
There are some example in the docs
For "better" options in this case #collection_select would be applicable, e.g.:
collection_select(:price, :product_id, Product.all, :id, :name, {:prompt => true}, :class => 'chzn-select')
"Better" is your call when it comes to Rails standard form helpers though. They tend to be rather inconsistent and have about umpteen ways of expressing the same thing, so just do what you're most comfortable with.

Formtastic non-model form, integration with external site, override/specify the input ID values

I'm using formtastic to collect information from a form and post dirctly to an external site.
I have no problem generating the form itself. However, since this is being submitted to an external site, they require that each input field have the specific IDs they specify, eg email or last_name -- not the closest Formtastic form, eg _email_input or _last_name_input.
I've looked at the Formtastic v1.2.3 code and I'm 90% sure the answer is "sorry, can't do that." I figured it couldn't hurt to check if I'm missing something. I would like some way to specify the ID completely, as in:
= semantic_form_for('', :url => "https://external_site.com/handler, :method => "post") do |form|
= form.input :last_name, :id => "last_name"
[etc]
Is this possible?
(I will note that I recognize that another, arguably superior approach would be to create an appropriate controller, sanity check the parameters locally, and dispatch the remote call from within the app only when it's well formed; however, that's not what I'm trying to do at the moment.)
Firstly i think you need to use semantic_fields_for for non-model forms. Next, to pass ids to each field, you can use the input_html options to specify them. for eg
form.input :email, :input_html => {:name => 'email', :id => 'email' }

Resources