1

I have a column in a table called Complete that is a boolean.

How can I (using the Rails 3 / JQuery way) provide a link that will toggle that via AJAX?

Historically, I've simply created my own jquery file, grabbed the click event, and done it by hand. But it really seems like I'm missing something by not doing it the "rails" way. If that makes sense.

I guess I still don't understand how the responds_to JS works, JS with ERB, etc.

Is there a good, up-to-date tutorial on how to do this?

Thanks.

1 Answer 1

2

see this post, Rails 3, Custom Actions, and HTML request methods

and use UJS on top of it.

so you have a fallback, if javascript is disabled

a possible method in the controller looks like that:

  # GET /surveys/1/close
  def close
    @survey.close!
    flash[:success] = "Survey successfully closed!"
    respond_to do |format|
      format.html { redirect_to(surveys_url) }
      format.xml  { head :ok }
      format.js { render :partial => 'list_item', :locals => { :survey => @survey } }
    end
  end

you could also use a state machine to change the state of your object.

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

2 Comments

+1 That method worked pretty well! I can now toggle the complete field using a POST link. I still don't quite get the whole JS ERB deal. Need to find a better explanation. Thanks
The .js.erb file is what gets rendered when the close action is accessed via javascript. With this file, you can make anything happen by using javascript/Rails in combination. Of course, toggling your value is a simple application, but you might use JQuery to highlight the changed field, or perhaps display some text to that effect as well. All of these can be done using the .js.erb file.

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.