Reputation: 20445
I would like to have a My_Events page, which only display events that current user subscribed to, and allow user to manipulate these events, in addition to Index page that displays all events in the system.
How should I go about this to comply with RESTful pattern? Should I just create a my_events action and my_event.html.erb, or create a new controller for it? What would be the reason to choose one over another approach?
Thank you.
Upvotes: 0
Views: 110
Reputation: 5626
If you happen to have a user controller, adding an events action to that and routing based on the user for user specific events would seem most RESTful.
# app/controllers/users_controller.rb
class UsersController < ApplicationController
...
def events
@user = User.find params[:id]
@events = @user.events
end
end
# config/routes.rb
resources :users do
member do
get 'events'
end
end
The benefit to this over something like /events/my_events is that the URL is unique to the data that is being presented. If a user uses the link /users/fubar/events then it will show events for the user fubar, where as with something like /events/my_events the generated page is tied to the session and not the URL.
Of course it really depends on what your goal is, and what requirements your application has.
Upvotes: 1
Reputation: 1082
You can have methods in another controller that load these Events and do all the things that you want/need. But if you want to use the index, edit, delete and create new Event, I would recommend having an events_controller as it will be better for you in the future when lines and lines of codes are added.
That's the point of having a controller per model, having everything ordered.
Upvotes: 0