简体   繁体   English

Rails新手:如何向Rails 3引擎添加路线?

[英]Rails newbie: How to add routes to a rails 3 engine?

I'm trying to write my first rails 3 gem - everything works well, except for routes - I can't seem to get them working. 我正在尝试编写我的第一个Rails 3 gem-一切正常,除了路线-我似乎无法使它们正常工作。 It's possible this is a very simple error - as mentioned, it's my first experience with engines. 可能这是一个非常简单的错误-如前所述,这是我第一次使用引擎。 The gem itself is very, very basic - literally just one scaffold 宝石本身非常非常基础-实际上只有一个脚手架

My gem's config/routes file: 我的宝石的配置/路由文件:

class ActionController::Routing::RouteSet
  resources :frogs
end

...And when I try to start the server, I get the following error: ...当我尝试启动服务器时,出现以下错误:

/home/john/.rvm/gems/ruby-1.9.2-p0/gems/cancandevise-0.1.0/config/routes.rb:3:in <class:RouteSet>': undefined method resources' for ActionDispatch::Routing::RouteSet:Class (NoMethodError) /home/john/.rvm/gems/ruby-1.9.2-p0/gems/cancandevise-0.1.0/config/routes.rb:3:in <class:RouteSet>': undefined method ActionDispatch的<class:RouteSet>': undefined method资源::路由:: RouteSet:Class(NoMethodError)

Any suggestions much appreciated. 任何建议,不胜感激。 At the present moment, the gem is nothing more than a very basic rails-generated 'frog' scaffold 目前,宝石只不过是一个非常基本的,由铁轨产生的“青蛙”支架

Cheers, - JB 干杯,-JB

@marcgg, I believe that's the syntax for a regular rails app, but I think he's talking about an engine. @marcgg,我相信这是常规Rails应用程序的语法,但是我认为他在谈论引擎。

@unclaimedbaggage, your engine/gem routes file should look like this: @unclaimedbaggage,您的引擎/ gem路由文件应如下所示:

Rails.application.routes.draw do |map|
  resources :frogs
end

I made an example engine that touches on all the common setup issues I encountered when creating my first gem, you might find it helpful to reference: 我制作了一个示例引擎,涉及创建我的第一个gem时遇到的所有常见设置问题,您可能会发现它对参考很有帮助:

http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/ http://keithschacht.com/creating-a-rails-3-engine-plugin-gem/

I'm not sure if I get why you're using a routeset. 我不确定是否能理解您为什么使用路由集。 What file did you show? 您显示了什么文件? Did you try this: 您是否尝试过:

   YourApp::Application.routes.draw do |map|  
     resources :frogs
   end

More info here: http://asciicasts.com/episodes/203-routing-in-rails-3 此处提供更多信息: http : //asciicasts.com/episodes/203-routing-in-rails-3

Just wanted to add an alternative here, as I'm not sure @Keith Schact is doing it the conventional way, this worked for me: 我只是想在这里添加一个替代方案,因为我不确定@Keith Schact是否以常规方式进行操作,这对我有用:

MyEngine::Engine.routes.draw do
  resources :frogs
end

then in the application that requires the gem: 然后在需要gem的应用程序中:

mount MyEngine::Engine => '/my_engine', :as => :some_namespace

The url you will get is then: 那么您将获得的网址是:

http://myserver.com/some_namespace/frogs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM