Ruby on Rails Sometimes Nested Resource -


in ruby on rails app i'm working on (2.3.8), have model user can have workouts

class workout < activerecord::base     belongs_to :user end 

i have trainer->client relationships (all tied user model through join table) user can add workouts himself, trainer can add workouts clients.

i've set routes follows:

map.resources :workouts map.resources :clients, :has_many => 'workouts' 

both sets of routes working (ie. /workouts , /clients/1/workouts). i've updated workouts_controller depending on if there client_id show 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 set views work correctly. depending on how got index want link differently add or edit screen. should conditionally...

<% if (@client.nil?) %>     <%= link_to 'new workout', new_workout_path %> <% else %>     <%= link_to 'new workout', new_client_workout_path(@client) %> <% end %> 

...or there easier way split these 2 cases out? seems i'll have lot of conditions in views , controllers , didn't know if way handle case or not.

this looks candidate creating link_to wrapping helper in workouthelpers can pass @client instance. if it's nil emit 1 link type otherwise emit other. can rid of conditionals in view , make more legible.

to more general , dry, pass in more info helper use right path helper such new_workout_path or edit_workout_path, etc

same suggestion apply general case have lot of similar conditional branching in views.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -