0

所以例如我有非restful命名路由:

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),或者我在这里做错了什么?

4

1 回答 1

0

你的rails版本是什么?

路线.rb

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

index.html.erb

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

测试控制器.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]

url127.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)
于 2013-10-31T15:20:45.853 回答