简体   繁体   English

Ruby on Rails,命名路线

[英]Ruby on rails, named routes

So for example i have non-restful named route: 因此,例如,我有一个非静态的命名路由:

get ':controller/:action/:juhu/:blabla', :as => "something"

and from rake routes i have following: 耙路,我有以下内容:

Prefix Verb URI Pattern                                  Controller#Action
something GET /:controller/:action/:juhu/:blabla(.:format) :controller#:action

//Controller //控制器

class TestsController < ApplicationController
    def juhu_juhu
        # Will try to render juhu_juhu.html.erb
    end
end

// View //查看

    <%= link_to "Get back", something_path %>

And i get error: 我得到错误:

No route matches {:juhu=>"1", :blabla=>"neta", :controller=>"tests", :action=>"juhu_juhu"} missing required keys: [:id]

So what about this "missing required keys: [:id]" 那么“缺少必需的键:[:id]”呢?

So i assume that every restful routes must have :id by convention(am i right?), but why non-restful routes also must have :id (literally named :id), or something i'm doing wrong here? 因此,我假设每条宁静的路线都必须具有:id (按惯例)(我对吗?),但是为什么非宁静的路线也必须具有:id (直译为:id),或者我在这里做错了什么?

What is your rails version? 您的rails版本是什么?

routes.rb routes.rb

root 'tests#index'
get ':controller/:action/:juhu/:blabla', :as => "something"

index.html.erb index.html.erb

<%= link_to "Get back", something_path %>

tests_controller.rb tests_controller.rb

class TestsController < ApplicationController
  def index
  end

  def juhu_juhu
  end
end

I try you example and I got this problem and check 127.0.0.1:3000 . 我尝试您的示例,但出现此问题并检查127.0.0.1:3000

No route matches {:action=>"index", :controller=>"tests"} missing required keys: [:juhu, :blabla]

The url 127.0.0.1:3000/tests/juhu_juhu/1/2 is success and could not have "missing required keys: [:id]" problem. 网址127.0.0.1:3000/tests/juhu_juhu/1/2是成功的,不会出现“缺少必需的键:[:id]”的问题。

And I link to 127.0.0.1:3000/tests/juhu_juhu/1/neta also success: 而且我链接到127.0.0.1:3000/tests/juhu_juhu/1/neta也成功:

Started GET "/tests/juhu_juhu/1/neta" for 127.0.0.1 at 2013-11-01 00:48:23 +0800
Processing by TestsController#juhu_juhu as HTML
  Parameters: {"juhu"=>"1", "blabla"=>"neta"}
  Rendered tests/juhu_juhu.html.erb within layouts/application (1.3ms)
Completed 200 OK in 82ms (Views: 66.1ms | ActiveRecord: 0.0ms)

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

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