0

たとえば、私は安らかな名前付きルートを持っています:

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

そしてレーキルートから私は以下を持っています:

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

あなたのレールのバージョンは何ですか?

ルート.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]

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 に答える