Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:17:07+00:00 2026-05-27T08:17:07+00:00

I have implemented an ajax-based button to add and remove a calendar event from

  • 0

I have implemented an ajax-based button to add and remove a calendar event from a user’s “wish list”. The button behaves as a toggle-switch. If the event is already on their wish list, it will remove it; if it is not, then it will add it.

My issue is that the button works correctly when the page is first loaded, but after clicking it and replacing it with a new button_to via AJAX, it no longer works.

I looked at this post but he has a slightly different architecture and I am not looking to completely refactor this just yet if I can avoid it. I have a separate model for the saved events. There are three models that come into play here: users, events, and saved_events. There is a many-to-many relationship between users and events which is represented as a saved_event. Here is the relevant code from the models.

user.rb:

  has_many :saved_events
  has_many :events, :through => :saved_events

event.rb:

  has_many :saved_events
  has_many :watchers, :class_name => "User", :through => :saved_events, :source => :user

saved_event.rb:

  belongs_to :event
  belongs_to :user

I have configured pretty standard routes.

routes.rb:

  resources :saved_events, :only => [:index,:create,:destroy]
  resources :users
  resources :events

I initially want to see this button an an event’s page, so I added it to the event’s show.html.erb. (I realize this isn’t quite DRY yet – I am waiting until it works correctly to move it into a partial)

events/show.html.erb:

  <% if !@is_watched %>
    <%= button_to "Add to Wishlist", { :action => 'create', :controller => 'saved_events', :id => @event.id }, :remote => true %>
  <% else %>
    <%= button_to "Remove from Wishlist", saved_event_path(@event), :remote => true, :method => 'delete' %>
  <% end %>

events_controller.rb:

  def show
    @event = Event.find(params[:id])
    @is_watched = @event.watchers.exists?(current_user) if !current_user.nil?

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @event }
      format.json { render :json => @event }
    end
  end

saved_events_controller.rb:

  def create
    logger.debug params.to_yaml

    @event = Event.find(params[:id])

    if !current_user.events.exists?(params[:id])
      current_user.events << @event
    end

    respond_to do |format|
      format.js
    end
  end

  def destroy
    logger.debug params.to_yaml

    @event = current_user.events.find(params[:id])
    current_user.events.delete(@event)

    respond_to do |format|
      format.js
    end
  end

My goal with the ajax was just to swap out the add form with the remove form (or vice-versa). So I have a create.js.erb and a destroy.js.erb which just use the same code from events/show.html.erb.

saved_events/create.js.erb:

$(".button_to").html("<%= escape_javascript button_to "Remove from Wishlist", saved_event_path(@event), :remote => true, :method => 'delete' %>");

saved_events/destroy.js.erb:

$(".button_to").html("<%= escape_javascript (button_to "Add to Wishlist", { :action => 'create', :controller => 'saved_events', :id => @event.id }, :remote => true) %>");

This is what I see in the log if the second button click is to “remove”

Started DELETE "/saved_events?id=1" for 127.0.0.1 at 2011-12-02 11:56:58 -0600

ActionController::RoutingError (No route matches [DELETE] "/saved_events"):

This is what I see in the log if the second button click is to “add”

Started POST "/saved_events/1" for 127.0.0.1 at 2011-12-02 11:59:12 -0600

ActionController::RoutingError (No route matches [POST] "/saved_events/1"):

On initial load of the page, the add button has “/saved_events?id=1” as its action, so I am confused as to why it would be “/saved_events/1” for the second button click.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:17:07+00:00Added an answer on May 27, 2026 at 8:17 am

    It seems the issue was from my jquery. I needed to wrap the form in a div, and then call jquery.html() on that div rather than the form itself. I did some other refactoring in the controllers, which I won’t post here for brevity, but here are my new js.erb files:

    create.js.erb:

    <% button_html = button_to( 'Remove from Wishlist', { :action => 'destroy', :id => @saved_event.id }, :remote => true, :method => 'delete' ) %>
    $("#toggle_wishlist").html("<%= escape_javascript( button_html ) %>");
    

    destroy.js.erb:

    $("#toggle_wishlist").html("<%= escape_javascript( button_to("Add to Wishlist", { :action => 'create', :controller => 'saved_events' }, :remote => true)) %>");
    $("#toggle_wishlist .button_to").append("<%= escape_javascript( hidden_field_tag :event_id, @event.id ) %>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented an IHttpAsyncHandler. I am making about 5 different AJAX calls from
I have already implemented some AJAX pagination in my Rails app by using the
I have implemented a simple file upload-download mechanism. When a user clicks a file
I have implemented tracing based on System.Diagnostics. I am also using a System.Diagnostics.TextWriterTraceListener, and
I have implemented an ajax-polling script that calls an action in the server Controller
I have a jquery calendar widget that do query several event sources on the
We have implemented a simple chat room feature in Rails using Simple Ajax updates.
I have to request data for a JS-script from a MySQL database (based upon
I have implemented what I thought was a pretty decent representation of MVC in
I have implemented a python webserver. Each http request spawns a new thread. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.