Ruby on Rails - Drop Down Box on Change Event

Rails Trying to submit a form onchange of dropdown

This is what I was able to do to get it to work. I named the form switch_car by using :name => "switch_car" and used the following javascript.

:onchange => ("javascript: document.switch_car.submit();")

I am still looking for a better answer so I will updated if I find something. This doesn't use submit .js for some reason. It processes it as HTML unlike the submit button which uses AJAX to update only the changing page elements. But this is the best I have been able to find so far.

Rails : Drop down in simple_form doesn't retain value when directed to edit path

You can using selected, like:

options_for_select(us_states, selected: "set_current_value")

More usage examples - options_for_select() docs.

select and onChange in a Ruby on Rails form

Modify your call to form.select, like this:

<%= form.select :Menu1, [["Option1","value1"],["Option2","value2"]], {}, 
:onChange=>"javascript: this.form.apply_button_name.click();" %>

Rails drop-down list without form_for

There are essentially a couple of ways to communicate from the browser to your rails application (and you want to do that, because the action of the administrator should change the state of your application).

One way is with a form. It does not need to be generated with form_for, you can use form_tag or just a plain HTML form tag (but in the latter case you will need to add all the CSRF extra fields yourself).

The other way is through javascript. You capture an event (in this case it would be a "change" event on your <select> tag. It does not need to be in a form for the event to be captured. Once you capture the event, you make an appropriate call (for instance with fetch, which is available in most browsers and also through polyfills) to the application.

From the receiving side, you will need to route a request to a corresponding action. For example

# in routes
resources :user_authorizations do
post :mark_in_review # will route /user_authorization/:id/mark_in_review
end

# controller
class UserAuthorizationsController
def mark_in_review
...
end
end

If you only want to allow changing from "pending" to "in review" (instead of both ways), you might just display a button instead of a select.

Youy might want to get the help of some small js framework like alpineJS or StimulusJS.

Something like:

import { Controller } from "stimulus"

class AuthorizationStatusChangeController extends Controller {
static values = {ident: Number}

markInReview() {
fetch(`/user_authorizations/${this.identValue}/mark_in_review`, {
credentials: "same-origin",
method: "POST"
})
}
}
<td data-controller="authorization-status-change"
data-authorization-status-change-ident-value="<%= authorization.id %>" >
<button data-action="change->authorization-status-change#markInReview">
Mark as "in review"
</button>
</td>

How to change the selected text in a drop down list in rails application based on a variable?

All you need to do is set the <option> element that has the country as selected='selected'. How you do that depends on how you built the option list.

For example, options_for_select takes the selected element as the 2nd argument.

options_for_select(['Alpha', 'Beta', 'Gamma'], 'Beta')
# => <option value="Alpha">Alpha</option>
# => <option value="Beta" selected="selected">Beta</option>
# => <option value="Gamma">Gamma</option>


Related Topics



Leave a reply



Submit