Borbat
Borbat

Reputation: 81

Rails Select Tag - passing an argument to controller

Recently I've encountered a problem with passing parameters from view to the controller using select tag. Here is the code:

/app/views/admins/heimdall.html.erb

<%= select_tag 'users',options_from_collection_for_select(User.where(id: @users.ids), "id", "email", params[:users].blank? ? current_user.id : params[:users]), :onchange => "this.form.submit();", class: 'form-control pull-right' %>

What I want is to pass the ID of the chosen user to the hash params in order to reload the page again, this time with the chosen user data.

In console I got this error:

heimdall:280 Uncaught TypeError: Cannot read property 'submit' of null

I will be very happy, if u help me.

Upvotes: 0

Views: 722

Answers (2)

Satishakumar Awati
Satishakumar Awati

Reputation: 3798

Please be make sure select_tag added inside form_tag or form_for view helper.

enter image description here

Upvotes: 2

Igor Drozdov
Igor Drozdov

Reputation: 15045

The problem is that this.form is undefined, because the js code is executed in document context, which doesn't have form property.

You can submit the form by using jquery selector $("form").submit() or maybe $("#email").parent("form").submit() if you have multiple forms and want to submit the form containing the email field.

Upvotes: 1

Related Questions