I am trying to get a hook on a select and set the selected option. I am struggling to do this because my drop down is generated in a loop so the id changes.
Normally I would do something like this
select product_template.name, :from => "product_product_template_id
What if the ID can change
e.g. product_product_presentations_attributes_0_presentation_id
Is there a way to leverage the select function and specify a css selector?
Thanks.
select also accepts label (as in the actual <label> text for that select) and name attributes for the :from option, so if those are not generated / changed all the time, you can use them for a less brittle test.
If that doesn't cut it, you can add a unique CSS class to your select and then select a specific option with something like:
find("select.new_css_class").find("option[value='THE OPTION VALUE YOU WANT']").select_option
Related
In Capybara, how can I refer to a select knowing only part of its name?
I have the following piece of HTML
<select name="user[work_hours][HASH][end_hour]">
...
</select>
Where HASH is an unknown value, and I need to find the select.
Can I use a regular expression?
There are many ways to do what you want. Depending on surrounding HTML you may be able to scope the find to a section of the page that only includes the one select so you don't need to specify anything to match the select at all
find('css to locate wrapping/scoping element').find(:select)
If that's not possible you can use CSS attribute begins-with and ends-with selectors like
find('select[name^="user[work_hours]["][name$="][end_hour]"]')
If that seems too complicated you can use the :element selector type which will allow you to match on any attribute with a string or regex. This may be inefficient on large pages with many select elements.
find(:element, :select, name: /user\[work_hours\]\[.*\]\[end_hour\]/
Another solution if the options in the select are unique on the page would just be to select the option and not worry about finding the actual select element
select('text of option')
I have two select tags/controls and I need the second select tag options to be dynamically generate based on the selection of first
For example I want to make after selecting a car brand in first select, the second must show the models of selected car brand, But I have not been able to do it please help
Please watch rails cast #88 dynamic select menus revised. The method you need to do this called grouped_collection_select
This method along with the proper coffee script (or JavaScript if you prefer that more than coffeescript) discussed in the video will complete exactly what you're looking for
I need to render a <select> with some values.
Those values correspond to a model that the rendered view's model belongs_to - e.g. Foo has_many Bar, I'm rendering Bar, and its form should include Foo's ids and names in the <select>.
That is no problem - right now I'm using simple_form for that.
But in addition to a fixed set of values, the user should be able to insert a new Foo aynchronously (in some popup / whatever), updating the <select> in the browser with the newly-generated id, and the given name.
That is no problem to implement by hand, but is there a Rails feature (like UJS) or plugin (like simple_form) which provides exactly that?
You can try selectize-rails
Examples can be found here. http://brianreavis.github.io/selectize.js/
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)
All,
BACKGROUND: I have a collection_select statement that displays a dropdown box.
OBJECTIVE: I'd like the dropdown to be an image that the user clicks to see the collection rather than the default box + down arrow that appears.
You need something like this plugin for jQuery. It allows you to replace a normal select with a custom one based on your wants. You would have to write your own select helper since you need to add the attributes to the select options that this plugin requires. If you get started and need help, post back here with what you're stuck on.