Rails 3 Form Element's id attr not unique - ruby-on-rails-3

What I use rails form helpers to build a form, input elements in that form have id attributes so they can match with their labels. The problem is this id is the something like person_first_name and not person_first_name_#{person.id} meaning if I have more than one form of the same type on the same page unexpected things can happen.
A perfect example of this is using jquery-ui datepicker. I have a series of forms all containing a text_field element wrapped in a div with the class datepicker. I apply the datepicker like this (in document ready) $('.datepicker input').datepicker(options) and guess what, every one of these elements, although has a seemingly working datepicker (click on input, datepicker appears), although when a date is selected in any of these datepickers only the first element on the page (of that element type, ex. input id=published_on) gets updated with the value.
Any suggestions on getting rails to output more unique element id's or make datepicker not use the id attribute?

You can easily customize the id of your input.
text_field(:person, :first_name, :id => "person_first_name_#{person.id}")

One would think that if you set the ID of the form to say :id => "edit_person_#{person.id}" that this ID would cascade downwards into the label and field logic. From what I can see, there's too much auto-magic in the way of adding an eloquent custom solution.

# Haml
= form_for #model do |f|
.field
= f.label :field_name
= f.text_field :field_name
When using the above example, ids are rendered as expected.

Related

How can I write a form to input a list of text with ActiveAdmin?

I'm writing an ActiveAdmin form for a record which takes an Array of Strings. Let's call them widgets. I'm using a multi select form like so:
ActiveAdmin.register Things do
permit_params widgets[]
form do |f|
f.inputs do
...
f.input :widgets,
as: :select,
collection: [],
multiple: true
end
end
end
With a bit of Javascript to help the user can enter their widgets and it will be delivered as an Array of Strings just fine. But when editing an existing record the existing record.widgets are not filled into the field, the widgets field is blank. I suspect what's happening is ActiveAdmin/Formtastic is interpreting this select as a has_many relationship with Widget, and the Strings as Widget IDs. ActiveAdmin is ignoring the failure.
How can I write an input to pass a plain Array of Strings into a model with ActiveAdmin? I'd like my parameters as params[:thing][:widgets] = ["foo", "bar"]
By providing collection: [], you are telling the form to always start with blank array.
You need to provide the actual Thing's widgets to the form. You'll need to modify the example to play well with the select box / javascript you wrote - just bear in mind that collection takes an array - collection: resource.widgets.split(',').

Simple_form association multiple

I am using a simpleform collection association input, as checkboxes in order to allow users to choose 1+ items from a list as part of an order. The list has a has_and_belongs_to_many association with the overall order. I want them to be able to do multiple of the same items, however. So I would want a small number input next to each checkbox. I can handle the javascript, I am just wondering how to do this with simpleform, if its even possible.
Thanks!
It is possible if you write a custom input for this specific task.
You would need to put it in # app/inputs/your_input.rb for Simple_form to automatically pick it up.
Then in the file :
class YourInput < SimpleForm::Inputs::Base
def input
# Your code here, but I just pasted the example that adds something after the existing field already as a reference.
"$ #{#builder.text_field(attribute_name, input_html_options)}".html_safe
end
end
and in the form :
f.input :money, as: :your
In case of checkbox, the method name will change to "check_boxes" from "input"

Show title instead of id in edit form

Say I got some lists and some tasks. Each list can have many tasks, a task belongs to a list. When I editing one of the tasks rails is showing the id of the corresponding list in the inputfield.
How is it possible to show the title of the list instead of the id?
You probably want to use a select input, something similar to this in your form code:
<%= f.select :list_id, List.all.collect { |l| [l.name, l.id] } %>
This will display the name of each List, but will actually assign the id to the task's list_id

Add extra data to a simple_form input

I'm using the simple_form gem.
I'm rendering an input based on a collection (a list of all my actiontypes)
<%= f.association :actiontype, collection: Actiontype.all, input_html: { data: {'impacts-pnl' => ??}} %>
I would like to be able to add a data-attribute to the input to store extra data.
In this case, I want to store the impacts_pnlattribute of my actiontype. The only problem is that I don't know how to refer to the current actiontype
collection.impacts_pnl doesn't work (obviously)
actiontype.impacts_pnlneither.
how can I pass this extra bit of data to my input?
If you want to add these attributes to the option-elements of a selectfield, you should alter the collection by using for example the .map() function. Also, use the input helper with block to do this, otherwise it doesn't work;
= f.input :actiontype do
= f.select :actiontype, Actiontype.all.map{|a| [a.name, a.id, {"data-impacts-pnl" => p.impacts_pnl}]}
For more information about this issue, see https://github.com/plataformatec/simple_form/issues/188

how to securely pass a related Class_id through a hidden form field

I have a GroupCoach model, Group Coaches has_many :groups. On my new Group form I want to pass a group_coach_id to the Group object in a hidden field so that a group gets associated with a GroupCoach without the user having to select one.
So in my Groups_Controller
#group = Group.new
#group_coach = GroupCoach.first(:order => "RAND()")
This will get a random GroupCoach. and then in the new Group view I have a hidden field
<%= f.hidden_field #group_coach %>
This obviously doesn't work 100% right. It does pass the group_coach_id but its not telling the form what column to save it in...
I have also heard this is very insecure...
Make a token column. Simply SHA1 encrypt it (or whatever your choice is) and pass that instead. It's much harder to guess.
I used the following code to resolve this issue
<%= f.hidden_field :group_coach_id, :value => #group_coach.id %>
But is this the most secure? Seems pretty insecure as I could change the value in Firebug or something...