1

I am new to Rails/Jquery and am really struggling.

What I want to do is change several fields in my database from null to true when a user clicks on a button (without navigating away from the current page). I know I need to use Ajax in some way or another, but am not sure how. I have been struggling for about 2 hours now and would love it if someone could even point me in the right direction.

Stackoverflow, you are my only hope.

1 Answer 1

4

I wrote an answer responding to a question similar to yours not long ago here. But basically what you have are 3 parts:

  • Your view that makes the AJAX request
  • Your controller that handles the AJAX request
  • and your .js.erb file that your controller will render

In your view, you will have something like

<%= button_to "Click me!", :action => "some_action", :remote => true %>

Which will send an AJAX request back to the server, in which you need to handle in your controller

def some_actoin
    // Update your database here
    respond_to do |format|
        format.js { render 'somepartial' }
    end
end

Then you will have a 'somepartial.js.erb' file that contains the code (in your case I don't know if this file needs to contain anything) that updates your DOM.

Read this for a very good explanation of AJAX and Rails3. Good luck.

Sign up to request clarification or add additional context in comments.

2 Comments

This totally covers it; but you (Spencer) should feel free to ask questions pertaining to your specific case as there are a plethora of ways to go about this.
I guess the part I am having trouble with is the some action part. Do I need to create root the corresponds with this action? I have used this method for a create action and got it to work. However, I have never used it just for something arbitrary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.