RoR:取消固定带有链接的帖子以将布尔属性更改为 false?



我希望有一个链接来使用户能够撤消固定的推文。

我的观点

<% if tweet.pin? %>
<%= link_to '<i class="fas fa-thumbtack"></i> Unpin'.html_safe, pin_tweet_path(tweet), :method => 
:put, class: "dropdown-item" %>
<% else %>
<%= link_to '<i class="fas fa-thumbtack"></i> Pin to your profile'.html_safe, pin_tweet_path(tweet), 
:method => :put, class: "dropdown-item" %>
<% end %>

我的模型 (tweet.rb)

belongs_to :user, optional: true
scope :pin,  -> { where(pin:true) }
scope :no_pin, -> (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

我的控制器方法

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

我的路线

resources :tweets do
member do
put :pin
end
end

此设置使每个引脚都可以在用户选择不同的引脚时自动替换,因此一次只有一个引脚。这工作正常。

但是我想包括一个选项,让用户也可以自己撤消引脚,因此没有引脚。

最简单的方法是什么?

更新

使用引脚控制器更新

resources :tweets do
...
resources :pins
...
end

路线

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

视图

<% if tweet.pin? %>
<%= link_to '<i class="fas fa-thumbtack"></i> Unpin'.html_safe, tweet_pin_path(tweet), method: :patch, class: "dropdown-item" %>
<% else %>
<%= link_to '<i class="fas fa-thumbtack"></i> Pin to your profile'.html_safe, tweet_pin_path(tweet), method: :patch, class: "dropdown-item" %>
<% end %>

错误

No route matches {:action=>"show", :controller=>"pins", :tweet_id=>#<Tweet id: 18, user_id: 2, content: "Pin test", created_at: "2020-11-23 15:15:03", updated_at: "2020-11-23 15:22:39", retweet_id: nil, pin: nil>}, missing required keys: [:id]

我最喜欢的博客文章之一是DHH如何组织他的Rails控制器。所以记住这一点,我想知道你为什么要采取pins行动。我认为如果你有一个TweetsController#update,甚至如果你有更多的功能,你的生活会容易得多,甚至可能Tweet::PinsController。使用更新函数,您可以只接受pins属性的值,而不是将其设置为 true。

class PinsController
def update
@tweet = Tweet.find(params[:id])
if @tweet.update(pin: params[:pin])
flash[:success] = 'Pinned tweet!'
redirect_to user_path(current_user.id)
else
flash[:error] = 'Ups, something went wrong here!'
redirect_to user_path(current_user.id)
end
end
end

然后只需更新您的路线和视图以使用update路线,应该没问题。无需更新模型代码。

最新更新