title: Toggle Button Action OnClick - JQuery + Rails
body: <p>I would like to implement a Subscribe/Unsubscribe toggle button which creates/destroys a Subscription objectonClick, but I'm having difficulties finding an elegant solution.</p>
  
  <p>I have experimented with multiple ideas, and have come to the conclusion that I need to use Javascript to accomplish this. However, I'm unsure if/how I can simultaneously change the button text and its action when a user clicks it. See <code>button_to 'Subscribe'</code> and <code>button_to 'Unubscribe'</code> below.</p>
  
  <p>Any feedback or advice would be much appreciated!</p>
  
  <p>Artists Index View:</p>
  
  <pre><code>&lt;% @artists.each do |artist| %&gt;
    &lt;tr id="tr_&lt;%=artist.id%&gt;"&gt;
      &lt;td&gt;&lt;%= artist.name %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= artist.artist_data["name"] %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= artist.artist_data["facebook_tour_dates_url"] %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= artist.artist_data["mbid"] %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= artist.artist_data["upcoming_event_count"] %&gt;&lt;/td&gt;
      &lt;% if artist.is_not_subscribed?(user_id: @user.id, artist_id: artist.id)%&gt;
        &lt;td&gt;&lt;%= button_to 'Subscribe', user_subscriptions_path(@user, artist_id: artist.id), method: :post, id: "subscribe_link_#{artist.id}", class: "subscribe", remote: true %&gt;&lt;/td&gt;
      &lt;% else %&gt;
        &lt;td&gt;&lt;%= button_to 'Unsubscribe', user_subscription_path(@user, artist.find_subscription(user_id: @user.id, artist_id: artist.id)), method: :delete, id: "unsubscribe_link_#{artist.id}", class: "unsubscribe", remote: true %&gt;&lt;/td&gt;
      &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Subscriptions Controller:</p>
  
  <pre><code>def create
    @user = User.find(params[:user_id])
    @subscription = @user.subscriptions.build(subscription_params)
    @subscription.artist_id = params[:artist_id]
  
  respond_to do |format|
    if @subscription.save
      format.html { redirect_to artists_path, notice: 'Subscription was successfully created.' }
      format.json { render :show, status: :created, location: @subscription }
      format.js
    else
      format.html { render :new }
      format.json { render json: @subscription.errors, status: :unprocessable_entity }
      format.js { render js: @subscription, notice:'Unable to create Subscription' }
     end
   end
  end
  
  def destroy
   @subscription = Subscription.find(params[:id])
   @subscription.destroy
  
   respond_to do |format|
    format.html { redirect_to user_subscriptions_url, notice: 'Subscription was successfully destroyed.' }
    format.json { head :no_content }
    format.js
   end
  end
  </code></pre>
  
  <p>Subscription Model:</p>
  
  <pre><code>class Subscription &lt; ActiveRecord::Base
    belongs_to :artist
    belongs_to :user
    accepts_nested_attributes_for :artist
  end
  </code></pre>
  
  <p>Artist Model:</p>
  
  <pre><code>class Artist &lt; ActiveRecord::Base
    include Subscribable
  
    has_and_belongs_to_many :events
    has_many :subscriptions
    has_many :users, through: :subscriptions
    validate :artist_already_exists? 
   end
  </code></pre>
  
  <p>User Model:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    include Subscribable
  
    has_many :subscriptions
    has_many :artists, through: :subscriptions
    accepts_nested_attributes_for :subscriptions
  end
  </code></pre>