繁体   English   中英

覆盖模型导轨3.1

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM