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 an开发者_如何转开发y sexier way to accomplish it ?
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.
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.
精彩评论