简体   繁体   中英

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.

I did a rails g model Post and trying to inherit the post model of forem by this line of code

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'

in my 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

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. 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:

module Forem
  class Post
    acts_as_votable
  end
end

And the same for 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.

forem hides your routes and and you have to mention main_app before the routes, so after writing

main_app.vote_up_post_path instead of vote_up_post_path the page was up again.

Hope it helps someone trying to use forem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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