rails命名空间路由和控制器 [英] rails namespace routes and controller

查看:332
本文介绍了rails命名空间路由和控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是不能弄清楚使用以下命名空间处理路由/控制器的最佳方式。
i'd只是想有以下设置:

  ... / manage / rooms / = @ rooms.number_of_rooms%> ;,<%= @ rooms.title%>)
... / manage / fuu /(<%= @ fuu.id%> ...)
..manage / foo / ...

我知道这是由
routes.rb

 命名空间:manage do 
资源:rooms,:fuu::foo
end

和以下... controller / manage / rooms_controller.rb fuu_controller.rb foo ...
示例:

  class Manage :: RoomsController< ApplicationController 

index
@rooms = Rooms.all
end
def create
@room = Room.new(room_params)
if @ room.save
redirect_to [:manage,@room]
else
render'new'
end
...
end

 

和控制器 controller / manage_controller.rb

  class ManageController< ApplicationController 
end

所以这里是我的问题我喜欢使用我的所有形式,变量 @ rooms.title ... 谁在 ... / manage / rooms / ... / manage / fuu / .... ... / manage / index.html.erb


下的
p>是通过控制器做最好的方式

解决方案 / div>

我会在这种情况下使用partials。如果它们都在该命名空间中共享,则该位置是manage views目录是有意义的。如果他们没有命名空间但仍在共享,我会在视图中创建一个共享目录。


i just can not figure out the best way to handle the routes / controller with the following namespace. i´d just like to have the following setup:

.../manage/rooms/  ( <%= @ rooms.number_of_rooms%>, <%= @ rooms.title %> )
.../manage/fuu/     ( <%= @ fuu.id %>...) 
..manage/foo/       ...

i know this is done by routes.rb

namespace :manage do
resources :rooms, :fuu, :foo
end

and under ...controller/manage/rooms_controller.rb and fuu_controller.rb and foo... example:

class Manage::RoomsController < ApplicationController

 index
 @rooms = Rooms.all
 end
 def create
 @room = Room.new(room_params)
  if @room.save
   redirect_to [:manage, @room]
  else
   render 'new'
 end
 ...
end

and a controller under controller/manage_controller.rb

class ManageController < ApplicationController
end

so here is my question i do like to use all of my forms and variables @rooms.title...who are under .../manage/rooms/ .../manage/fuu/ .... under the .../manage/index.html.erb

is the best way to do it via the controller e.g. render partial or changing the controller which the routes point to?

thanks!!!

解决方案

I would use partials in this situation. If they are all shared in that namespace, it makes sense for the location to be the manage views directory. If they weren't namespaced but still being shared, I'd create a 'shared' directory in views.

这篇关于rails命名空间路由和控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆