开发者

How to make these rails routs

I'm trying to make some routes to view and manage missions in an app. The missions belong to organizations. My idea is to do something like this:

organizations/1/missons                 #(index) list of missions of the organization
organizations/1/missions/1                  #show a mission that belongs to an organization
organizations/1/admin/missions              #list of missions in a new view that has the commands to admin the missions
organizations/1/admin/missions/1/edit         #edit the mission
organizations/1/admin/missions/1/destroy    #destroy the mission
missions                                    #all missions of all organizations
missions/1                                  #show mission page

I don't really know if this is a good开发者_Python百科 way to rout a system like this or if it's overkill in a way. For now I have the standard routing working:

resources :organizations do
    resources :missions
end

But I wish to have some more views. What's the best way to route this, and how many controllers to keep them lean and with few actions?


As always there are several ways to accomplish this. I would probably go with two different controllers for the regular missions, perhaps like this:

resources :organizations do
  resources :missions, :only => [:index, :show]
  resources :admin_missions
end
resources :missions, :only => [:index, :show]

This would of course result in "...admin_missions..." instead of "...admin/missions..." but I think it is the easiest way to do it. This will use three controllers where MissionsController is called from two different locations but you can check what to display by checking if the params[:organization_id] is present or not.

Edit

If you decide to use the same controller for all missions and for organizations, here is an example for how the index action could start:

def index
  if params[:organization_id]
    @organization = Organization.find(params[:organization_id])
    @missions = @organization.missions.all
  else
    @missions = Mission.all
  end

  ...
end

But if you want to have different views for them, then it might be better to separate to two controllers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜