Topic: Nested Routes in Controller - please comment my code

I have two models in my application: User has_many Results.

I want the the URL /results/ to return all Results, and the URL /users/MrSmith/results/ to return just the Results for User "MrSmith".

This is what my code looks now:

class ResultsController < ApplicationController

  def index
    @user = User.find_by_login(params[:user_id])
    if @user.nil?
      @results = Result.find(:all)
    else
      @results = @user.results
    end
  end

end


This works, but can it be improved?

Thanks!

Tom.

Re: Nested Routes in Controller - please comment my code

Looks clean enought to me. If you have multiple parents, such as a 'revisions' controller that handles revisions for both pages and articles, you could do something like this: http://pastie.org/pastes/250861

But if it's "no parent" or "user", your code is more than good enough.

Re: Nested Routes in Controller - please comment my code

OK thanks leethal!

Re: Nested Routes in Controller - please comment my code

when using nested resource, getting the parent item through a before filter is generally a good idea, as you would end up duplicating the User.find in other actions otherwise:

class ResultsController < ApplicationController
  before_filter :get_parent

  def index
    @results = @user.nil? ? Result.find(:all) : @user.results
  end

private
  def get_parent
    @user = User.find_by_login(params[:user_id]) unless params[:user_id].blank?
  end
end


PS: Moved to "Refactoring" as that's what this is wink

Re: Nested Routes in Controller - please comment my code

With the possibility of redirecting to several controllers based on the parent's existence and parent's type, is there a best practice for managing the numerous redirect possibilities?

Thanks,
Tom

Re: Nested Routes in Controller - please comment my code

you'd be better off opening a new thread for that question and describe your requirements for thisa bit further.

Re: Nested Routes in Controller - please comment my code

Duplex wrote:

you'd be better off opening a new thread for that question and describe your requirements for thisa bit further.

I argee
---------------------------------------
nuoc hoa | nuoc hoa nam | nuoc hoa nu

Re: Nested Routes in Controller - please comment my code

Duplex wrote:

when using nested resource, getting the parent item through a before filter is generally a good idea, as you would end up duplicating the User.find in other actions otherwise:

class ResultsController < ApplicationController
  before_filter :get_parent

  def index
    @results = @user.nil? ? Result.find(:all) : @user.results
  end

private
  def get_parent
    @user = User.find_by_login(params[:user_id]) unless params[:user_id].blank?
  end
end


PS: Moved to "Refactoring" as that's what this is wink

thank for your information
--------------------------------------
thong tin ngan hang | dongabank | Vietcombank | Vietinbank | acbbank

Re: Nested Routes in Controller - please comment my code

good! Thanks for sharing information
-----------------------------------------
quang cao online | quang cao | SEO | online makerting

Last edited by CCronaldo (2010-03-25 23:43:19)

Re: Nested Routes in Controller - please comment my code

Thank leethal for sharing information
-------------------------------------------------
du hoc uc

Re: Nested Routes in Controller - please comment my code

I think it is not necessary to add params[:user_id].blank?, because in that case it would be nil too.
And I tend to use try method in that case:

class ResultsController < ApplicationController
  before_filter :get_parent
  def index
    @results = @user.try(:results) || Result.find(:all)
  end
private
  def get_parent
    @user = User.find_by_login params[:user_id]
  end
end

Re: Nested Routes in Controller - please comment my code

Thanks for code man. Good share