duplicated id for fields using simple_form - ruby-on-rails

I've a form in my page for each record, something like this:
- records.each do |r|
...
= simple_form_for record do |f|
= f.input :field
The problem is that each input has the same id. How can I add a prefix to generate ids like record_12_field? I've tried input_html and html options on the simple_form_for attributes but doesn't work.
The only alternative I can think of is manually setting the id for each input.
This is different to the duplication suggested, because i'm not asking how to set an id for a single field, but how to add a prefix to all of them.
The answer is using the namespace option, this answer does not appear on the duplicate. Please allow me to answer this.

Update
As Arnold mentioned, it is possible with the namespace option.
Original answer
I think it is not possible to add the prefix automatically. As a workaround you can try with:
- records.each do |record|
...
= simple_form_for record do |f|
= f.input :field, input_html: { id: "record_#{record.id}_field" }
Alternatively, you can switch to form_for and use this solution.

Related

Field gets radio button's item id instead of its value

I have snippet:
<%= f.input :purpose, as: :radio_buttons, collection: category.subcategories,
wrapper: :vertical_collection_inline %>
which lines values of category.subcategories horizontally how I want
The problem is, when I select either of option, it assigns that option's ID, but not its value.
How should I refactor the code?
Using IDs has advantages as warned in the comments, however what you’re trying to do should work with either:
category.subcategories.collect(:&values)
Where values is the name of the field which hols “Rent” etc.
The more railsy way to do this is with collection_radio_buttons, like this:
f.collection_radio_buttons(:purpose, category.subcategories, :value, :value)
Again where “value” is the field name.

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 to create a custom method for the hint parameter of the input method in ActiveAdmin/Simple Form gems?

From this code:
ActiveAdmin.register News do
form do |f|
f.inputs do
f.input :url
f.input :site_name
f.input :site_url
f.input :image,
as: :file,
hint: if f.object.image_url
f.template.image_tag(f.object.image_url)
else
''
end
end
f.actions
end
end
I want to extract the if clause in a method, since I have this part repeating a lot. The question is - how exactly do I do that? To clarify:
Where do I define the method - in some helper, in a new file? How do I make sure ActiveAdmin has access to this file? Do I have to create a custom ActiveAdmin controller action?
Although something like some_method(f, field) (field is in my case image) is acceptable, I would like to have it in a more generic form, so that I repeat myself less, like f.hint with the hint method inferring the field name as the first parameter of f.input. Is this possible?
Any advice on this is appreciated.
Probably the easiest way would be to simply create your own formtastic input for images that either set the hint option by default, or add in extra HTML to show an image preview. Doing so might allow you to do something like this:
f.input :image, as: :image, preview: f.object.image_url

semantic_field_for for arbitrary attribute

I am quit new to rails. I have been using formtastic in my project and I find it quite easy to deal with the form objects. I have a small problem which I hope to clear out here.
I want to create a form for arbitrary objects and a has_many type of nested form for it. What I mean is semantic_form_for does not use any model instead uses symbol for creating form and this form now has to have a to_many type of semantic_fields_for. This is how my code looks,
= semantic_form_for :company do |f|
= f.inputs "company" do
= f.input :name
= f.input :enterprise_code
= f.semantic_fields_for :email do |e|
= f.inputs "email" do
= f.input :address
The form above is not associated to any model. I will pick these attributes in the controller and assign it individually. The email fields in the form has to be like has_many. Now, it is like one to one. How can this be achieved.
I used formtastic only once so i might be wrong but i don't think there's anything mentioned that you can use it only for one type of association. It only says that you for nested forms we can use semantic_fields_for :model and then both model's must be setup correctly with accepts_nested_attributes_for

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.

Resources