Two or more controllers sharing same voting action (just the model class differing) - how to create a mixin to be included in all controllers?

I'd rather go the polymorphic way but for your mixin (module) a solution can be this.

I'd rather go the polymorphic way but for your mixin (module) a solution can be this: Questions Controller include Voting Voting Module: module Voting def vote_up @voteable = Object. Const_get(self.class. To_s.

Chomp('Controller'). Singularize). Find(params:id) current_user.

Vote_exclusively_for(@voteable) respond_to do |format| format. Js { render :action => 'votes/vote_completed' } end end end.

Thanks for your quick reply. I've tried that approach, however I always get the following error: NoMethodError (undefined method find' for QuestionsController:Class): lib/voting. Rb:3:in vote_up'.

I've included voting on my QuestionsController and removed the vote_up method from there. – Bruno Feb 25 at 12:34 Updated answer, can you try this new approach? Thanks – tommasop Feb 27 at 16:07 2 Just a note — self.class.

To_s. Chomp('Controller') is quite a bit faster than using gsub. It's a better option anytime you need to chomp a set of characters off the end of a string.

– coreyward Feb 27 at 16:42 Edited answer accordingly – tommasop Feb 27 at 18:03 tommasop and coreyward, it worked perfectly. Thanks! Now I just need to figure out a nicer way to create the vote_up and vote_down routes for the members of voteable models.Is it possible to do some meta programming on the routes file, so that we could inject the get 'vote_up' and get 'vote_down' routes using a single line of code?

Many thanks! – Bruno Feb 27 at 21:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions