我有一个 Like 模型,它连接用户喜欢的项目。除了标准liked
状态,项目可以是spinned
& pinned
,都是布尔字段。
喜欢模型
_________________________________________________
| item_id | user_id | liked | spinned | pinned |
|---------|---------|---------|----------|--------|
| 1 | 1 | true | true | false |
| 2 | 1 | false | true | true |
| 3 | 2 | true | false | false |
-------------------------------------------------
一个项目的不同状态都通过同一个AJAX
调用更新,action
在 my likes controller
. 当同类尚不存在时,它会在所需状态为真的情况下创建同类。
喜欢控制器
def index
# get the item id
action = params[:act]
mid = params[:mid]
# gets the desired like item to see if it already exists
like = Like.where(:item_id => mid, :user_id => current_user.id).first
case action
# like the item
when "like"
# if the item doesn't exist
if like.blank?
# create the like
Like.create(:user_id => current_user.id, :item_id => mid, :liked => true)
else
# else toggle the liked status
like.toggle! :liked
end
# pin the item
when "pin"
if like.blank?
# create the like
Like.create(:user_id => current_user.id, :item_id => mid, :pinned => true)
else
# else toggle the pinned status
like.toggle! :pinned
end
# spin the item
when "spin"
if like.blank?
# create the like
Like.create(:user_id => current_user.id, :item_id => mid, :spinned => true)
else
# else toggle the spinned status
like.toggle! :spinned
end
end
end
如您所见,代码case
变得非常重复,我想知道是否可以action
更有效地使用我的 ('like', 'pin', 'spin') 变量。
我可以action
通过从我的view
.
然后我可以使用它来只有一个代码块而不是 3 个类似的代码块吗?
IE
action = "liked" # this would change depending on the desired action
# if the item doesn't exist
if like.blank?
# original
Like.create(:user_id => current_user.id, :item_id => mid, :liked => true)
# doesn't work, how would I do this appropriately?
Like.create("user_id = ?, item_id = ?, ? = true", current_user.id, mid, action)
else
# this works!, what about SQL injection? How can I prevent this?
like.toggle!("#{action}")
end