Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForumPost acts_as_votable #22

Open
perlmunger opened this issue Dec 3, 2019 · 6 comments
Open

ForumPost acts_as_votable #22

perlmunger opened this issue Dec 3, 2019 · 6 comments

Comments

@perlmunger
Copy link

This may be a rails question more than simple discussion (again, sorry), but is it possible to add something like acts_as_votable to the ForumPost model from within my rails apps rather than within simple_disucssion itself?

@excid3
Copy link
Owner

excid3 commented Dec 3, 2019

Yeah, totally. Should just add the gem to your model and customize the views for it. You'll probably need to build your own links and controller action for the votes up and down, but shouldn't be too hard.

@perlmunger
Copy link
Author

Right. I've customized views and controllers already. (BTW, thanks for this great gem. It's really intuitive and super simple to customize). I guess (lack of Rails fluency showing here) I'm not sure exactly what you mean by "Should just add the gem to your model". Are you saying I create my own model that wraps the same table as the SimpleDiscussion ForumPost model does? Or is there a way to just create an extension to that class and add the acts_as_votable to it?

@excid3
Copy link
Owner

excid3 commented Dec 3, 2019

You'll need to re-open the ForumPost class from the gem so that you can add acts_as_votable to it.

The easiest way is probably to add an initializer and define your code there. I think something like this should reopen the class safely.

# config/initializers_simple_discussion.rb
class ForumPost
  acts_as_votable
end

@perlmunger
Copy link
Author

So I think you intended for that file path to be # config/initializers/simple_discussion.rb, right?

I tried it in the intializers directory and I get:

config/initializers/simple_discussion.rb:2:in <class:ForumPost>': undefined local variable or method acts_as_votable' for ForumPost:Class (NameError)

when I attempt to run the rails server. This is rails 6, so I may be running into something specific to my configuration.

(I made sure acts_as_votable is in the Gemfile and successfully bundle installed. I also successfully ran its migration)

Not sure. Any ideas?

@excid3
Copy link
Owner

excid3 commented Dec 4, 2019

Yes, and you probably have to inherit from ApplicationRecord then if you're getting that error.

# config/initializers/simple_discussion.rb
class ForumPost < ApplicationRecord
  acts_as_votable
end

@epetkus
Copy link

epetkus commented Nov 29, 2020

@excid3 I know this is an old one - I am actually on the same boat as this original thread. So I wont to make ForumThread to have act_as_voteable and as per your last suggestion, I am adding a new initializer to add act_as_voteable but then it seems like any other gems are not available. Example error:

undefined method `friendly' for #<Class:0x0000000005531a10> Did you mean? friendly_id?

Is there a way to load all the gems before config/initializers/simple_discussion.rb? (I realize this is more of me being bad at rails than anything else!) Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants