Rails route.rb语法 [英] Rails routes.rb syntax

查看:113
本文介绍了Rails route.rb语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了,但是找不到在Rails 3中拼写route.rb语法的页面。虽然有一些准则,概述甚至是高级示例,但是为什么没有一个页面可以拼出确切的语法每个关键字?本页

I have searched and searched and I cannot find a page which spells out the syntax of routes.rb in Rails 3. There are guidelines, overviews, even advanced examples but why isn't there a page that spells out the exact syntax of each keyword?? This page

http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/

包含许多高级示例,但没有花时间讨论给出的所有示例的行为。如果有人可以指出我指向破坏route.rb语法的页面,我将不胜感激。

contains a lot of advanced examples but doesn't take the time to discuss the behavior of all the examples given. I would appreciate it if someone could point me to a page that breaks down the syntax of routes.rb.

这是我要解决的问题。我有两个模型modelA和modelB。关系是modelA has_many modelB和modelB属于modelA。我在modelA的命名空间下为modelB创建了控制器。所以在我的rails应用文件夹中,我有

Here is the problem I am trying to solve. I have two models modelA and modelB. The relationship is modelA has_many modelB and modelB belongs_to modelA. I created the controller for modelB under namespace of modelA. So in my rails app folder, I have

app/controllers/modelA_controller.rb
app/controllers/modelA/modelB_controller.rb

我希望自己的路线是这样的:

I want my routes to be as such:

http://localhost:3000/modelA/:modelA_id/modelB/  [index]
http://localhost:3000/modelA/:modelA_id/modelB/:modelB_id  [show]
etc.

我在routes.rb中尝试了以下操作并且都无效:

I tried the following in routes.rb and none of it works:

resources :modelA do
  resources :modelB
end
--
resources :modelA do
  member do
    resources :modelB
  end
end
--
namespace :modelA do
  resources :modelB
end
--
match '/modelA/:modelA_id/modelB/action', :to => '/modelA/modelB#action'

我知道我尝试过的某些事情显然是错误的,但是当您已经花了2天时间解决一个问题,一切顺利!

I know some of the things I tried are obviously wrong but when you have spent 2 days on a single problem, anything goes!

推荐答案

没人对路由有权威性的指导语法是非常灵活的,因此您可能只针对该主题写了几章。但是,我建议: http://guides.rubyonrails.org/routing.html

The reason no one has a "definitive" guide on routing syntax is that it is pretty flexible so you could probably write a few chapters just on that one subject. However, I would recommend: http://guides.rubyonrails.org/routing.html

从您的问题来看,听起来您是在 modelA modelB $ c>,但您还希望 modelA id 放在路线本身内。

From your question, it sounds like you're namespacing modelB under modelA but you also want the id for modelA to be within the route itself.

因此,如果您的 ModelBController 看起来像这样:

So if your ModelBController looks something like:

class ModelA::ModelBController < ApplicationController
  # controller code here
end

然后您可以做:

resources :modelA do
  resources :modelB, :module => :modelA
end

但是,您确定要像这样对控制器命名空间吗?如果您只想嵌套资源,例如典型的 has_many 关系,则无需在<$ modelB 下命名空间c $ c> modelA 。

However, are you sure you want to namespace the controller like that? If you just want nested resources like a typical has_many relationship, you don't need to be namespacing modelB under modelA.

相反,您需要:

/app
  /controllers
    /modelA
      # some files
    /modelB
      # some files

而您的 modelB 控制器将是:

class ModelBController < ApplicationController
  # controller code here
end

那么您可以做

resources :modelA do
  resources :modelB
end

这篇关于Rails route.rb语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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