简体   繁体   English

覆盖模型导轨3.1

[英]Overriding a model rails 3.1

I am trying to override a model of forem gem so that I could use thumbs_up gem for voting purpose. 我试图覆盖forem gem的模型,以便我可以使用thumbs_up gem进行投票。

I did a rails g model Post and trying to inherit the post model of forem by this line of code 我做了一个rails g模型Post并尝试通过这行代码继承forem的post模型

class Post < Forem::Post

    acts_as_voteable
end

same for the controller 同样适用于控制器

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

I keep getting this error 我一直收到这个错误

undefined method `vote_up_post_path' 未定义的方法`vote_up_post_path'

in my route.rb 在我的route.rb

 mount Forem::Engine, :at => "/forums"


resources :posts do
  member do
    post :vote_up
  end
end

I guess I am doing something really stupid out here and I am not overriding the model correctly. 我想我在做一些非常愚蠢的事情并没有正确地覆盖模型。 I was using this Clarification on how to use "thumbs_up" voting gem with Rails 3 post to set up thumbs_up 我正在使用这个澄清,如何使用Rails 3 post的“thumbs_up”投票宝石来设置thumbs_up

Can someone help?? 有人可以帮忙吗?

If I'm getting your question correctly, you wanna change the behavior of forem Post in order to support voting using acts_as_votable. 如果我正确地得到你的问题,你想改变forem Post的行为以支持使用acts_as_votable进行投票。 For that to work you need to re-open Forem::Post class in an initializer (eg config/initializers/forem.rb) and add to it acts_as_votable line like this: 为此,你需要在初始化程序中重新打开Forem :: Post类(例如config / initializers / forem.rb)并添加到acts_as_votable这样的行:

module Forem
  class Post
    acts_as_votable
  end
end

And the same for Forem::PostsController: 和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

It seems it was a stupid mistake, realized it while having discussion with patrickmcgraw. 这似乎是一个愚蠢的错误,在与patrickmcgraw讨论时意识到了这一点。

forem hides your routes and and you have to mention main_app before the routes, so after writing forem隐藏你的路线,你必须在路线前提到main_app,所以写完之后

main_app.vote_up_post_path instead of vote_up_post_path the page was up again. main_app.vote_up_post_path而不是vote_up_post_path页面再次启动。

Hope it helps someone trying to use forem. 希望它可以帮助有人试图使用forem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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