【问题标题】:Overriding a model rails 3.1覆盖模型导轨 3.1
【发布时间】:2012-02-06 03:48:55
【问题描述】:

我正在尝试覆盖 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

我想我在这里做了一些非常愚蠢的事情,而且我没有正确地覆盖模型。我正在使用这个Clarification on how to use "thumbs_up" voting gem with Rails 3 帖子来设置thumbs_up

有人可以帮忙吗?

【问题讨论】:

  • 路径方法是从routes.rb生成的。您是否有用于 vote_up 操作的路线?
  • @patrickmcgraw 我已经用路线信息更新了问题,请看一下
  • 好的,那么当你运行 rake 路由时你会得到什么?
  • @patrickmcgraw vote_up_post POST /posts/:id/vote_up(.:format) {:action=>"vote_up", :controller=>"posts"}
  • 那一切看起来都很好。未定义的方法异常究竟是在哪里引发的?

标签: ruby-on-rails model vote-up-buttons


【解决方案1】:

这似乎是一个愚蠢的错误,在与 patrickmcgraw 讨论时意识到了这一点。

forem 隐藏了你的路线,你必须在路线之前提到 main_app,所以在写完之后

main_app.vote_up_post_path 而不是vote_up_post_path 页面又重新启动了。

希望它对尝试使用 forem 的人有所帮助。

【讨论】:

    【解决方案2】:

    如果我没有正确回答您的问题,您想更改 forem Post 的行为以支持使用acts_as_votable 进行投票。 为此,您需要在初始化程序(例如 config/initializers/forem.rb)中重新打开 Forem::Post 类,并像这样添加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
    

    【讨论】:

    • 我应该把这段代码放在哪里?因为如果我把它放在初始化程序中,即 config/initializers/forem.rb。我的前端路径本身变得无法识别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多