title: RoR: Unpinning a post with link to change boolean attribute to false?
body: <p>I'm looking to have a link to enable a user undo a pinned tweet.</p>
  <p>My view</p>
  <pre><code> &lt;% if tweet.pin? %&gt;
          &lt;%= link_to '&lt;i class=&quot;fas fa-thumbtack&quot;&gt;&lt;/i&gt; Unpin'.html_safe, pin_tweet_path(tweet), :method =&gt; 
          :put, class: &quot;dropdown-item&quot; %&gt;
      &lt;% else %&gt;
          &lt;%= link_to '&lt;i class=&quot;fas fa-thumbtack&quot;&gt;&lt;/i&gt; Pin to your profile'.html_safe, pin_tweet_path(tweet), 
          :method =&gt; :put, class: &quot;dropdown-item&quot; %&gt;
      &lt;% end %&gt;
  </code></pre>
  <p>My model (tweet.rb)</p>
  <pre><code>    belongs_to :user, optional: true
  
      scope :pin,  -&gt; { where(pin:true) }
       scope :no_pin, -&gt; (id) { where.not(id: id) if id }
      
       after_save :ensure_only_one_pinned_tweet
      
     private
       def ensure_only_one_pinned_tweet
         user.tweets.pin.no_pin(id).update_all(pin: false) if pin?
       end
  </code></pre>
  <p>My controller method</p>
  <pre><code>def pin
      @tweet = Tweet.find(params[:id])
      if @tweet.update(pin: true)
        redirect_to user_path(current_user.id)
      else
        @tweet.update(pin: false)
        redirect_to user_path(current_user.id)
      end
    end
  </code></pre>
  <p>My route</p>
  <pre><code>resources :tweets do
      member do
        put :pin
      end
    end
  </code></pre>
  <p>This setup enables each pin to be automatically replaced on the user choosing a different pin thereby only having one pin at the one time. This works fine.</p>
  <p>But I want to include the option for the user to undo a pin themselves as well and thus have no pins.</p>
  <p>What's the easiest way to do this? ty</p>
  <p><strong>UPDATE</strong></p>
  <p>Update with pin controller</p>
  <pre><code>resources :tweets do
  ...
      resources :pins
  ...
  end
  </code></pre>
  <p>Routes</p>
  <pre><code>tweet_pins GET    /tweets/:tweet_id/pins(.:format)                                                         pins#index
                                        POST   /tweets/:tweet_id/pins(.:format)                                                         pins#create
                          new_tweet_pin GET    /tweets/:tweet_id/pins/new(.:format)                                                     pins#new
                         edit_tweet_pin GET    /tweets/:tweet_id/pins/:id/edit(.:format)                                                pins#edit
                              tweet_pin GET    /tweets/:tweet_id/pins/:id(.:format)                                                     pins#show
                                        PATCH  /tweets/:tweet_id/pins/:id(.:format)                                                     pins#update
                                        PUT    /tweets/:tweet_id/pins/:id(.:format)                                                     pins#update
                                        DELETE /tweets/:tweet_id/pins/:id(.:format)                                                     pins#destroy
  </code></pre>
  <p>View</p>
  <pre><code>&lt;% if tweet.pin? %&gt;
    &lt;%= link_to '&lt;i class=&quot;fas fa-thumbtack&quot;&gt;&lt;/i&gt; Unpin'.html_safe, tweet_pin_path(tweet), method: :patch, class: &quot;dropdown-item&quot; %&gt;
  &lt;% else %&gt;
    &lt;%= link_to '&lt;i class=&quot;fas fa-thumbtack&quot;&gt;&lt;/i&gt; Pin to your profile'.html_safe, tweet_pin_path(tweet), method: :patch, class: &quot;dropdown-item&quot; %&gt;
  &lt;% end %&gt;
  </code></pre>
  <p>Error</p>
  <pre><code>No route matches {:action=&gt;&quot;show&quot;, :controller=&gt;&quot;pins&quot;, :tweet_id=&gt;#&lt;Tweet id: 18, user_id: 2, content: &quot;Pin test&quot;, created_at: &quot;2020-11-23 15:15:03&quot;, updated_at: &quot;2020-11-23 15:22:39&quot;, retweet_id: nil, pin: nil&gt;}, missing required keys: [:id]
  </code></pre>