views:

161

answers:

2

I have a model Whitelabel and a User has_many :whitelables

I have a custom method current_whitelabel (like authlogic or restful_auth for current_user)

I want my users to manage their whitelabels (ie: edit_whitelabels_path(id)).

But I don't want to send the whitelabel ID in params when it refers to the current_whitelabel.

So my idea is to create two resources: map.resources whitelabels and map.resource whitelabel.

But I don't like this so much. Is there any sexier way to accomplish it ?

A: 

In your controller action you can do this:

class WhitelabelsController < ActionController
  def edit
    @whitelabel = params[:id] ?  Whitelabel.find(params[:id]) : current_whitelabel
    redirect_to whitelabels_url unless @whitelabel
    ....
  end
  ...
end

Now rails will treat /whitelabel/edit as /whitelabel/edit/#{current_whitelabel.id} without specifying the id.

If this happens for more than one action you can do it as a before filter. Just be sure to remove all @whitelabel = Whitelable.find(params[:id]) lines from the individual actions.

class WhitelabelsController < ActionController
  before_filter :select_whitelabel, :except => [:index, :new]

  def select_whitelabel
    @whitelabel = params[:id] ?  Whitelabel.find(params[:id]) : current_whitelabel
    redirect_to whitelabels_url unless @whitelabel
  end
  ...
end

Answering the more clearly stated question in the comment: You can use a singular resource in tandem with the above code to have the effect you want.

config/routes.rb

map.resource :my_whitelabel, :controller => "whitelabels", :member => {:dashboard => :get}

Then in the whitelabels controller use the above code. This keeps things DRY by using the same controller for different paths with the same actions. The resource defines a dashboard action, so you'll have to add that to the controller too. But if you're using the before_filter version there should be no problem.

EmFi
Thanks for your answer EmFi, but it doesn't answer to my question. Maybe my question is not clear.I have some actions for all whitelabels but some which are just for the current one.so I need two types of urls:/whitelabels/id/action for all user's whitelabels.And something like /my_current_whitelabel/dashboard.Do I have to do two different resources to do that ?
louis
Your question was definitely not clear. I've updated my answer to give a solution to the problem mentioned in your comment.
EmFi
Ok, thanks EmFi I thought that there was a more rails way to do this but maybe not. So I will do named route.
louis
I jumped the gun with the named_route, and revised the answer to a more DRY and robust approach
EmFi
Ok EmFi, after a long night, I am going to develop my whitelabels system and I realized that your solution is to create TWO new routes : a single one and a multiple one. Exactly what I don't want to do ... But maybe there is no other way.
louis
I know you're using subdomains to set the current whitelabel, but... out of curiosity, what is wrong with having two routes?
EmFi
+1  A: 

Ok I finally solved my problem.

Each whitelabel has his own subdomain (thanks to subdomain_fu), so I just need a single resource whitelabel in my routes to do action on my current_whitelabel and if I want to do action on others whitelabels, I just have to switch the subdomain.

Thanks EmFi for trying to answer to my strange question.

louis