Maaz Siddiqui


50 Experience
0 Lessons Completed
0 Questions Solved


Posted in How to process form_with using GET request as XHR

I'm working on a Rails 6 app, and want to update page view based on a dropdown value selected using XHR. Dropdown must use GET method coz I am calling index action.

I am using form_with which by default uses remote: true. I am not using local: true.

I tried onchange: ", 'submit')" - this does send XHR request and receives a response but does not update view.

I tried onchange: "this.form.submit();" - this does a full page reload not utilizing XHR.

Code from app/views/users/index.html.erb

<%= form_with url: station_users_path(station_id: Current.user.home_station), method: :get do |form| %>
          <%= :user_status, options_for_select( { "Active users" => "unlocked", "Inactive users" => "locked"}, @user_status ), {}, { :onchange => ", 'submit')" } %>
        <% end %>

Code from app/controllers/users_controller.rb

  def index
    @user_status = params[:user_status] || "unlocked"
    @users = @station.users.send(@user_status) || []
    @user_status == "unlocked" ? seperate_managers_from_users : @managers = []