Skip to main content

How to process form_with using GET request as XHR

Rails • Asked by Maaz Siddiqui

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 = []

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.