Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to process form_with using GET request as XHR

Maaz Siddiqui asked in Rails

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: "Rails.fire(this.form, '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| %>
          <%= form.select :user_status, options_for_select( { "Active users" => "unlocked", "Inactive users" => "locked"}, @user_status ), {}, { :onchange => "Rails.fire(this.form, '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 = []
  end
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 76,990+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.