In a Ruby on Rails app I'm working on (2.3.8), I have a model User that can have Workouts
class Workout < ActiveRecord::Base
belongs_to :user
end
I also have Trainer->Client relationships (all tied to the User model through a join table) A user can add workouts for himself, BUT a trainer can also add workouts for his clients.
I've set up the routes as follows:
map.resources :workouts
map.resources :clients, :has_many => 'workouts'
Both sets of routes are working (ie. /workouts and /clients/1/workouts). I've updated the workouts_controller so that depending on if there is a client_id I will show a different set of workouts
def index
if(params[:client_id])
@workouts = Workout.find_all_by_user_id(params[:client_id])
else
@workouts = Workout.find_all_by_user_id(@current_user.id)
end
My question is, how do I set up my views to work correctly. Depending on how I got to index I want to link differently to the add or edit screen. Should I just do everything conditionally...
<% if (@client.nil?) %>
<%= link_to 'New workout', new_workout_path %>
<% else %>
<%= link_to 'New workout', new_client_workout_path(@client) %>
<% end %>
...or is there some easier way to split these two cases out? It seems like I'll have a lot of conditions in the views and controllers and didn't know if this is the way to handle this case or not.