How do I submit a form with a particular field? - ruby-on-rails

Need to select and submit a form on a page containing many different forms, which has a hidden field with a particular value.
I know there's form.fields_with() to select form fields, and page.form_with(), to select forms with particular attributes, but I want to select a form which has a hidden field with the value attribute 'xxxxx', for example.
Is there a way of doing this in Mechanize? Or am I stuck using xpath or a hack solution? The XPath for what I want is
xpath("//form[div/input/#value='xxx']").click_button
Of course I cannot click_button on an xpath, though.

You could do:
page.form_with :form_node => page.at(xpath)

Related

Rails dropdown menu to select object for text_field

I am building a rails form and have an interesting problem I am trying to solve. I can't seem to find anything online to point me in the right direction. Thank you.
Is it possible to use a dropdown menu to select the :object_name for a text field?
In my head, I am picturing a collection_select form helper nested within a text_field form helper, though not sure this is possible.
In the form, I'd like the user to select the proper :object_name from an array
[:object_1, :object_2, :object_3, :object_4]
then give that entry a value with the text field
text_field(object_name, method, options = {})
The objects are all db columns in the same model.
Yes you can do that using jquery.
On change of the object names dropdown value, change the name attribute of the text field.
$('#selectObjectName').change(function(){
var field = document.getElementById("id-of-the-text-field-to-be-changed");
field.setAttribute("name", "value-came-from-the-selected-dropdown");
})

Submit all values in a multiple select box, even when they're not selected (Rails)

I have a multiple select box in a form
<%= f.select :receipts, [], {}, multiple: true -%>
I want to ALWAYS post all options that are in the select box, but HTML only posts the ones the user has clicked on.
I could handle the submit event in JavaScript and set all the options to be selected, but I don't really want the user to see the items get selected. The form may fail to post and I'd prefer not to change anything visual on the screen.
The other thought I had was to use a hidden element and set its value to be a comma separated list of items from the multi-select box just before the submit happens. Then I don't have to mess with the selection state of the multi-select box.
Is there some Rails magic I can use instead of doing this?

Using render as the value_method in Rails Simple Form?

I'm working on an RoR project with the Simple_Form gem. For those who don't know, simple form creates easier form helpers for use in views. My question is regarding the f.association helper which allows users to easily select associations on models via a select box, radio buttons or checkboxes.
f.association has an option called value_method which allows you to pass a method for generating the name of the individual models in the association to choose from. The default is just to_s, so it will work with select boxes. However, I'm using radio buttons, and I'd like to know if anyone knows of a way to use the render partial method as the value_method.
In my setup, I have a User model with a user.haml partial that I'd like to be rendered next to each checkbox, that way users selecting other users can have any easy to recognize user div with picture, name, and last login to choose.
Thanks, steakchaser answered my question with his comment. This is the solution:
f.association :some_association, label_method: lambda { |obj| render obj }
I realized label method is better than value method, because even with checkboxes and radio buttons, simple form still put the values in the options value="..." attribute. This caused an html rendering error.

Is it possible to have date_select with year text box?

I'm looking to create an ancestry type webpage with a date field that allows user to enter a date going back to thee digits. This makes date_select with dropdown boxes and the jquery datepicker very un-user friendly.
Does anyone know a good way to display a text box for the year but a dropdown for month and day?
I'm hoping to use the "intelligence" of date_select while still allowing the user to enter the year in a more user friendly fashion (the textbox).
Thanks for any help you can give me!
With great respect to the developers of Rails, the date_select usability is pretty weak for almost any case :-)
Unlike other field types, the date_select helper generates three select tags with special names and ids, one each for the year, month, and day elements. It's the naming convention that allows controller code to auto-magically re-assemble the inputs into a (single) date when it is processing the params array.
Sorry, I don't have a handy example of the naming format (since I never use date_select), but if you look at the names of the fields, you might be able to mimic the behavior without too much hackery by using the :discard_year option. That gets you the month and day fields, and a hidden field (I think) containing the current year.
If you're not averse to a little JS or CoffeeScript, you could modify the input field after the DOM is loaded, by removing the hidden "type" attribute (thus making it a simple text field) and setting its value to be empty.
I would choose between three options
1-Use a date_select only for day and month and a text field with some javascript for the year
Date_select accepts a :discard_year option, if you set it to true, the year field is rendered as a hidden_field so there's no select/dropdown http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-date_select
Then you put a text_field and bind to the keyUp event and modify the value of the hidden year field
The most important problem about this is that, if the user has javascript disabled, the value of the text field will never be set to the hidden field and that's a problem.
2-Use a date_select only for day and month and a text field
I'm not sure if you can do this option, you can try using the same date_select as above and set the name of the input field with the name of the year field. The problem is that date_select will put a hidden field with that name and you put another field with the same name and the value sent on submission may not be what you want... You should see if it's posible.
3-Use a date_select and add extra functionality using Choosen http://harvesthq.github.com/chosen/
Put the date_select as always and set the start_year and end_year that you want, then replace the selects with choosen selects, the choosen select includes a text field to search between the years of the select
Choosen gives you a nice and consistence look almost crossbrowser (didn't test it on old IE versions), you get the text input for user-friendliness and, if javascript is disabled, the user still have the three selects.
I would definitelly use number 3, but maybe you don't want to add a plugin.
Experimenting with the Javascript route suggested by Tom, I came up with something like the following:
<%= f.date_select :birth_date %>
<script>
var date_field =
document.getElementsByName('ancestor[birth_date(1i)]')[0];
var new_html = '<%=
f.text_field 'birth_date(1i)', :value=>(f.object.birth_date.strftime("%Y") rescue "") %>';
date_field.outerHTML = new_html;
</script>
(Improvements welcome)

What it the rails way to make dynamic forms? (Is the JQuery the only option?)

I'm going to make form that:
Shows a message (text, object description) to rhe user
Shows a list of buttons. like 'comment', 'reroute' and 'close'.
3.1 If the user clicks on 'comment', shows text_area for input. The input is not for an attribute from a model, it must be later appended to the 'log' field.
3.2 If the user clicks on 'reroute', shows collection_select to choose to whom to reroute.
3.3 If the user clicks on 'close', shows text_area for input. The input must be later saved to the 'solution' field of the model.
What is the rails way to do this? Should I start learn the JQuery or there some gems instead of it? Or maybe I don't need nothind special and such a thing can be done with Rails alone?

Resources