[英]Voting in voter controller for multiple models passing polymorphic/duck typed object variable
我正在嘗試對各種模型進行投票:角色模型,宇宙模型。 我通過單個DRY渲染的部分在各自的顯示模板上有一些投票按鈕。 這意味着當我將模型傳遞給創建投票記錄的控制器邏輯時,我需要對該模型進行部分不可知的處理。
所以是的,我想知道是否有可能將這種鴨子類型的變量傳遞給投票控制器,而不是在每個Character and Universe控制器中重復投票創建邏輯。
如何使用路徑助手,投票局部和投票控制器使此類鴨子類型變量傳遞? 還是我應該在每個可移植模型中重復代碼?
僅供參考:自然,兩個模型的投票是多態的。 我正在使用acts_as_votable gem https://github.com/ryanto/acts_as_votable
_vote.html.haml
.vote-comment-buttons
= link_to image_tag("upvote.png"), votes_upvote_path(), method: :post, remote: true
%span=# @comment.get_upvotes.sum(:vote_weight)
= link_to image_tag("downvote.png"), votes_downvote_path(), method: :post, remote: true
^是否將鴨子類型變量傳遞到路徑?,如果是,如何/在哪里定義鴨子類型變量?
(注意:我正在嘗試通過AJAX進行投票,但可以忽略與ajax相關的代碼,例如remote:true)
character.rb
class Character < ActiveRecord::Base
acts_as_votable # creates a polymorphic association with votes
end
universe.rb
class Universe < ActiveRecord::Base
acts_as_votable # creates a polymorphic association with votes
end
votes_controller.rb
class VotesController < ApplicationController
before_action :check_if_voted(@votable)
def upvote
#votable.vote_up current_user
end
def downvote(votable)
#votable.vote_down current_user
end
end
在動作上設置參數是否有效? 還是只使用params []?
在動作上設置參數是否有效? 還是只使用params []?
您不能在公共操作中使用參數,應從params []解析所有數據
如何使用路徑助手,投票局部和投票控制器使此類鴨子類型變量傳遞? 還是我應該在每個可移植模型中重復代碼?
假設兩種模型的投票程序相同,請考慮使用關注點 :
配置/ application.rb中
module YourApplicationName
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/app/controllers/concerns)
end
end
應用程序/控制器/關切/ voting_controller.rb
module VotingController
extend ActiveSupport::Concern
included do
before_filter :obtain_resources, only: [:upvote, :downvote]
end
def upvote
# here goes your upvoting logics, something like
# @object.liked_by user1, :vote_weight => 1
# the @object variable has already been set in obtain_resource filter
# other params should be catched up from the params hash
end
def downvote
# here goes your downvoting logics
end
private
def obtain_resources
# here we retrieve the name of a particular controller which triggered this action
model = controller_name.singularize.camelize.constantize
@object = model.find(params[:id])
end
end
如果您更喜歡使用有意義的變量名而不是@object
,則可以執行以下操作:
# app/controllers/concerns/voting_controller.rb
def obtain_resources
model = controller_name.singularize.camelize.constantize
instance_name = controller_name.singularize
# here you get @universe or @character
instance_variable_set "@#{instance_name}", model.find(params[:id])
end
然后,在您的控制器中,您應該包括以下問題:
class UniverseController < ApplicationController
include VotingController
# that's all, you should not implement voting actions here as they are included from VotingController
end
class CharactersController < ApplicationController
include VotingController
end
您還應該確保在config / routes.rb文件中設置適當的路由,以便兩個控制器中都存在upvote
和downvote
操作。 如您已經了解的那樣,您應該將upvote_universe_path
或upvote_characters_path
傳遞給您的部分服務器,以使此工作有效(與下一次upvote_universe_path
相同)。 每個路由都應將對象的id
傳遞給控制器的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.