title: Rails AJAX request to controller method that then calls javascript function
body: <p>I have looked at dozens of stack overflow posts and haven't found a solution that works which is why I'm reaching out for an otherwise well documented use case. </p>
  
  <p>I have a button that that should do this. </p>
  
  <ul>
  <li>When clicked, call a custom controller method that updates the model and does other things</li>
  <li>Call a javascript function to update the page without reloading it (ajax) </li>
  </ul>
  
  <p>Right now, the only way I am able to call a custom controller method is via this way which feels really hacky. I have stripped this down to as simple as possible. </p>
  
  <p>routes.rb</p>
  
  <p><code>match 'admin/:id/toggleAdmin' =&gt; 'admin#toggleAdmin', via: [:patch, :put], as: :toggleAdmin</code></p>
  
  <p>list.html.erb</p>
  
  <p><code>&lt;td&gt;&lt;%= link_to "Toggle Admin", toggleAdmin_path(id: user.id), method: :patch %&gt;&lt;/td&gt;</code></p>
  
  <p>admin_controller.rb</p>
  
  <pre><code>class AdminController &lt; ApplicationController
  
    def toggleAdmin
      idToToggle = User.find(params[:id]).id
      if idToToggle == current_user.id
        redirect_to admin_list_path, danger: "You tried to make yourself a normal user! Don't do that!"
      else
        User.find(params[:id]).updateToAdmin()
        redirect_to admin_list_path, info: "The user with an ID of #{idToToggle} has had their admin attribute toggled!"
      end
    end  
  </code></pre>
  
  <p>end</p>
  
  <p>What I would like to do instead of reloading the page when an admin is toggled is to just use some javascript to rewrite that part of the dom. </p>
  
  <p>What is a better way to go about this? </p>
  
  <p>Here are just a few of the various resources I have already tried.  </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/10307581/how-to-call-a-controllers-method-from-a-view">How to call a controller&#39;s method from a view?</a></li>
  <li><a href="https://stackoverflow.com/questions/8906527/can-we-call-a-controllers-method-from-a-view-as-we-call-from-helper-ideally">Can we call a Controller&#39;s method from a view (as we call from helper ideally)?</a></li>
  <li><a href="https://stackoverflow.com/questions/22514956/call-controller-method-with-rails-ajax">Call controller method with rails-Ajax?</a></li>
  <li><a href="https://www.reddit.com/r/rails/comments/7iyp6g/want_a_button_to_call_a_method_apparently_this_is/" rel="nofollow noreferrer">https://www.reddit.com/r/rails/comments/7iyp6g/want_a_button_to_call_a_method_apparently_this_is/</a></li>
  <li><a href="https://stackoverflow.com/questions/22937676/how-do-i-call-a-javascript-function-from-an-html-erb">How do I call a JavaScript function from an html.erb</a></li>
  </ul>
  
  <p>Thanks for the help.</p>
  
  <p>--- Edit for more information. </p>
  
  <p>Using a clean rails app I now have the ability to call a controller method more cleanly but I am not getting an ajax request to go through that updates the page to show that the action was completed. (I am expecting a boolean value change and a flash). Here is the following relevant code: </p>
  
  <p>users.js</p>
  
  <pre><code>$("#edit-form").html("&lt;%= j render partial: 'form', locals: { user: @user } %&gt;")
  </code></pre>
  
  <p>_form.html.erb</p>
  
  <pre><code>&lt;%= form_for user do |form| %&gt;
    &lt;%= user.admin %&gt;
  &lt;% end %&gt;
  &lt;%= link_to "Toggle Admin", toggle_admin_user_path(user), method: :put, remote: true %&gt;
  </code></pre>
  
  <p>edit.html.erb</p>
  
  <pre><code>&lt;h1&gt;Editing User&lt;/h1&gt;
  
  &lt;div id="edit-form"&gt;
    &lt;%= render partial: 'form', locals: { user: @user } %&gt;
  &lt;/div&gt;
  
  &lt;%= link_to 'Show', @user %&gt; |
  &lt;%= link_to 'Back', users_path %&gt;
  </code></pre>
  
  <p>users_controller.rb Note that I have only included the <code>toggle_admin</code> and a couple other methods as the rest are just scaffolding. </p>
  
  <pre><code>class UsersController &lt; ApplicationController
    before_action :set_user, only: [:show, :edit, :update, :destroy, :toggle_admin]
  
    def toggle_admin
      if 1 == 1
        logger.info "This is from info"
        u = User.find(params[:id])
        u.admin = !(u.admin)
        u.save
        respond_to do |format|
          format.js { flash[:info] = "The user with an ID of #{@user.id} has had their admin attribute toggled!" }
        end
      else
        redirect_to admin_list_path, danger: "You tried to make yourself a normal user! Don't do that!"
      end
    end
  
    private
      # Use callbacks to share common setup or constraints between actions.
      def set_user
        @user = User.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
      def user_params
        params.fetch(:user, {})
      end
  end
  </code></pre>