Ruby on rails,命名路由

时间:2013-10-31 15:03:40

标签: ruby-on-rails ruby

所以例如我有非宁静的命名路线:

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

rake路线我有以下内容:

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

//控制器

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

//查看

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

我得到错误:

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

那么“缺少必需的键:[:id]”

所以我假设每条宁静的路线必须按照约定:id (我是对的吗?),但为什么非宁静的路线也必须有:id (字面意思命名:id),或者我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

你的rails版本是什么?

的routes.rb

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

index.html.erb

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

tests_controller.rb

class TestsController < ApplicationController
  def index
  end

  def juhu_juhu
  end
end

我试试你的例子,我遇到了这个问题并检查了127.0.0.1:3000

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

网址127.0.0.1:3000/tests/juhu_juhu/1/2成功,无法“缺少必需的密钥:[:id]”问题。

我也链接到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)