[英]Overriding a model rails 3.1
我試圖覆蓋forem gem的模型,以便我可以使用thumbs_up gem進行投票。
我做了一個rails g模型Post並嘗試通過這行代碼繼承forem的post模型
class Post < Forem::Post
acts_as_voteable
end
同樣適用於控制器
class PostsController < Forem::Postscontroller
def vote_up
begin
current_user.vote_for(@post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
我一直收到這個錯誤
未定義的方法`vote_up_post_path'
在我的route.rb
mount Forem::Engine, :at => "/forums"
resources :posts do
member do
post :vote_up
end
end
我想我在做一些非常愚蠢的事情並沒有正確地覆蓋模型。 我正在使用這個澄清,如何使用Rails 3 post的“thumbs_up”投票寶石來設置thumbs_up
有人可以幫忙嗎?
如果我正確地得到你的問題,你想改變forem Post的行為以支持使用acts_as_votable進行投票。 為此,你需要在初始化程序中重新打開Forem :: Post類(例如config / initializers / forem.rb)並添加到acts_as_votable這樣的行:
module Forem
class Post
acts_as_votable
end
end
和Forem :: PostsController相同:
module Forem
class PostsController
def vote_up
begin
current_user.vote_for(@post = Post.find(params[:id]))
render :nothing => true, :status => 200
rescue ActiveRecord::RecordInvalid
render :nothing => true, :status => 404
end
end
end
end
這似乎是一個愚蠢的錯誤,在與patrickmcgraw討論時意識到了這一點。
forem隱藏你的路線,你必須在路線前提到main_app,所以寫完之后
main_app.vote_up_post_path
而不是vote_up_post_path
頁面再次啟動。
希望它可以幫助有人試圖使用forem。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.