2
\$\begingroup\$

I have a Rails app and in it I have two models, widget and votes. Votes are used to keep track of the desirability of a widget.

In my models I have:

class Vote < ActiveRecord::Base
 belongs_to :widget
end
class Widget < ActiveRecord::Base
 has_many :votes
end

I am trying to figure out the best way to route votes up and votes down. At the moment I am leaning towards this:

resources :widgets do
 member do
 put :vote_up
 put :vote_down
 end
end

Where the respective functions reside in the widgets' controller and it creates and saves votes in that controller. However, I realize that this doesn't really fit correctly with the MVC idea (votes should be created and built in their own controller). Is this acceptable or should I be doing something more like:

resources :widgets do
 resources :votes do
 member do
 put :vote_up
 put :vote_down
 end
 end
end
rolfl
98.1k17 gold badges219 silver badges419 bronze badges
asked Jul 9, 2013 at 19:13
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

I would not create custom controller actions. up and down are attributes of a Vote, so I would do the following:

resources :widgets do
 resources :votes, only: [:new, :create]
end

And then send a POST to VotesController#create with params[direction: 'up'] and have an attribute on Vote called direction.

Then validate Vote#direction can only be up or down:

class Vote < ActionController:Base
 validates_inclusion_of :direction, in: %w(up down)
end 

Your check for this kind of routing decision is to run rake routes and make sure the route aliases make sense, in this case we are really creating widget_votes, which looks like:

 Prefix Verb URI Pattern Controller#Action
 widget_votes POST /widgets/:widget_id/votes(.:format) votes#create
new_widget_vote GET /widgets/:widget_id/votes/new(.:format) votes#new
answered Feb 17, 2014 at 17:51
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.