我在這裏面臨一種棘手的挑戰,也許你們中的一個人可以幫助我做到這一點。沒有太難,我只是不知道要走的好路。這裏說到:多態行爲與link_to
我有2種型號:
Activity
和Comment
我在這裏使用MongoID有一個著名的寶石來處理票(喜歡)。它只關注在Activity
或Comment
中嵌入投票模型。我們沒有Like
模型。
我多麼希望能夠「喜歡」「活動」和/或「評論」。我的猜測是使用名爲LikesController
的單個控制器與2種方法來同時喜歡或不喜歡一個項目。
我會再有這樣的路線:
resources :activities, :only => [] do
resources :comments, :only => [:create, :destroy]
resources :likes, :only => [:create, :destroy]
end
resources :comments, :only => [] do
resources :likes, :only => [:create, :destroy]
end
這裏是給定的路線從bundle exec rake routes
:
activity_comments POST /activities/:activity_id/comments(.:format) comments#create
activity_comment DELETE /activities/:activity_id/comments/:id(.:format) comments#destroy
activity_likes POST /activities/:activity_id/likes(.:format) likes#create
activity_like DELETE /activities/:activity_id/likes/:id(.:format) likes#destroy
comment_likes POST /comments/:comment_id/likes(.:format) likes#create
comment_like DELETE /comments/:comment_id/likes/:id(.:format) likes#destroy
現在我想有一個「贊」 /「與」鏈接在某些視圖頁面上指向LikesController
上的相應動作(create => like; destroy =>不像)。
唯一的是我不知道如何創建具有良好路徑的link_to
。 這是我到目前爲止有:
link_to 'Like', eval("#{item.class.name.downcase}_likes_path(item)"), :method => 'post'
link_to 'Unlike, eval("#{item.class.name.downcase}_likes_path(item)"), :method => 'destroy'
誰能給我如何做,在一個更清潔的方式小費了嗎?
這裏是我的模型:
class Activity
include Mongoid::Document
include Mongoid::Timestamps
include Mongo::Voteable
has_many :comments, :as => :item
belongs_to :subject, :polymorphic => true
embeds_one :user
validates :user, :presence => true
voteable self, :up => +1, :down => -1
end
class Comment
include Mongoid::Document
include Mongoid::Timestamps
include Mongo::Voteable
belongs_to :item, :polymorphic => true
embeds_one :user
field :text, :type => String, :default => nil
validates :text, :presence => true
validates :user, :presence => true
attr_accessible :text, :user
voteable self, :up => +1, :down => -1
end
你要記住,很多即將成爲的對象可能是「可愛」也,這就是爲什麼我選擇了LikesController
去其內部的各個副本方法旁邊物品的控制器來處理「喜歡」和「不喜歡」。也許我錯了 ?
謝謝。
是的,我也有這個想法,我只是不喜歡嵌套IF ELSE條件^^但我想也許你沒有選擇><。我會等待別人回答,但是thx。 – 2012-03-20 18:32:00