使用名称空间admin作为子文件夹添加控制器 [英] adding controllers with a namespace admin as a subfolder

查看:109
本文介绍了使用名称空间admin作为子文件夹添加控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ROR 3.2上有一个简单的cms. 使用此文件夹方案:

app |控制器|我的控制器

但是我想有一个管理员"部分,在那里我也可以有一些控制器. 所以我创建了

rails生成控制器管理员/用户

app |控制器| admin&我的管理员

所以我的文件是:

users_controller.rb
class Admin::UsersController < ApplicationController

  def index
    render(:text => "sou o index!")
  end

  def list
    render(:text => "sou o list")
  end

end

在我的路线上,我有:

namespace :admin do
    resources :users
  end

match ':controller(/:action(/:id))(.:format)'

我是Rails的新手,我不知道解决方案.无法在任何地方找到它.

问题是 我尝试访问:

http://localhost:3000/admin/users/list

我得到这个错误:

未知动作找不到动作显示" 管理员:: UsersController

解决方案

默认情况下,您似乎对Rails的RESTful路由的工作方式并不了解.我建议阅读《 Rails指南》中的资源路由部分 .默认情况下,在路径中使用resources时,show操作是用于显示特定模型记录的操作.您可以自定义此行为,以便可以更改URL show操作的名称,而不是模型中的方法名称:

resources :users, :path_names => { :new => 'list' }

如果要使用RESTful路由(应使用),则应删除默认路由(match ':controller(/:action(/:id))(.:format)').另外,您可以随时在终端上运行rake routes,以查看有关当前路由配置的详细信息.

i have a simple cms on ROR 3.2. with this folder scheme:

app |controllers |my controllers

but i wanted to have an "admin" section where i could have some controllers too. so i created

rails generate controller admin/Users

app | controllers |admin & my admin controllers

so my file is:

users_controller.rb
class Admin::UsersController < ApplicationController

  def index
    render(:text => "sou o index!")
  end

  def list
    render(:text => "sou o list")
  end

end

On my routes i have:

namespace :admin do
    resources :users
  end

match ':controller(/:action(/:id))(.:format)'

Im new to rails and i cant figure out the solution. Cant find it anywhere.

The PROBLEM is i try do acess:

http://localhost:3000/admin/users/list

and i get this error:

Unknown action The action 'show' could not be found for Admin::UsersController

解决方案

You seem to not have an understanding of how Rails's RESTful routing works by default. I recommend reading the Resource Routing section of the Rails Guides. By default, when using resources in your routes, the show action is what is used to display a particular model record. You can customize this behavior to an extent in that you can change the URL that for the show action, but not the method name in the model:

resources :users, :path_names => { :new => 'list' }

If you are going to use RESTful routing (which you should), you should remove the default route (match ':controller(/:action(/:id))(.:format)'). Also, you can run rake routes at any time from the terminal to see details about your current routing configuration.

这篇关于使用名称空间admin作为子文件夹添加控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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