2012-06-29 57 views
0

我對Rails完全陌生,並且在嵌套資源/模型工作時遇到了很多麻煩。每當我嘗試提交表單時,我都會收到路由錯誤。Rails 3 - 使用嵌套資源提交表單時發生路由錯誤

No route matches {:action=>"show", :controller=>"tests", :test_suite_id=>#<Test id: 8, name: "1", test_type: "1", result_type: "1", input: "1", created_at: "2012-06-29 07:01:11", updated_at: "2012-06-29 07:01:11", date: "1", test_suite_id: 4>} 

爲什麼有太多test_suite_id的出現在異常輸出,爲什麼行動「作秀」,而不是「指數」有人能解釋一下嗎?當我提交表單時,我想回到index/test_suites /:test_suite_id/tests。

下面是所有相關代碼:

的routes.rb

resources :test_suites do 
    resources :tests 
    end 

tests_controller.rb

class TestsController < ApplicationController 
    # GET /tests 
    # GET /tests.json 
    def index 

     @testsuite = TestSuite.find(params[:test_suite_id]) 
     @tests = @testsuite.tests 

     respond_to do |format| 
      format.html # index.html.erb 
      format.json { render json: @tests } 
     end 
    end 

    # GET /tests/1 
    # GET /tests/1.json 
    def show 
     @testsuite = TestSuite.find(params[:test_suite_id]) 
     @test = @testsuite.tests.find(params[:id]) 

     respond_to do |format| 
      format.html # show.html.erb 
      format.json { render json: @test } 
     end 
    end 

    # GET /tests/new 
    # GET /tests/new.json 
    def new 
     @testsuite = TestSuite.find(params[:test_suite_id]) 
     @test = @testsuite.tests.build 

     #@test = Test.new 
     @test.build_hardware 
     @test.build_software 

     respond_to do |format| 
      format.html # new.html.erb 
      format.json { render json: @test } 
     end 
    end 

    # GET /tests/1/edit 
    def edit 
     @test = Test.find(params[:id]) 
    end 

    # POST /tests 
    # POST /tests.json 
    def create 

     @testsuite = TestSuite.find(params[:test_suite_id]) 
     @test = @testsuite.tests.build(params[:test]) 

     respond_to do |format| 
      if @test.save 
       flash[:notice] = "Test Added Succesfully" 
       format.html { redirect_to [:test_suite, @test], notice: 'Test was successfully created.' } 
       format.json { render json: @test, status: :created, location: @test } 
       else 
       format.html { render action: "new" } 
       format.json { render json: @test.errors, status: :unprocessable_entity } 
      end 
     end 
    end 
end 

_form.html.erb

<%= form_for([:test_suite, @test], :action => "index") do |f| %> 
    <% if @test.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@test.errors.count, "error") %> prohibited this test from being saved:</h2> 

     <ul> 
     <% @test.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 
    <br /> 
    <h2>Test information</h2> 

    <div class="field"> 
    <%= f.label :name %><br /> 
    <%= f.text_field :name %> 
    </div> 
    <div class="field"> 
    <%= f.label :test_type %><br /> 
    <%= f.text_field :test_type %> 
    </div> 
    <div class="field"> 
    <%= f.label :result_type %><br /> 
    <%= f.text_field :result_type %> 
    </div> 
    <div class="field"> 
    <%= f.label :date %><br /> 
    <%= f.text_field :date %> 
    </div> 
    <div class="field"> 
    <%= f.label :input %><br /> 
    <%= f.text_area :input %> 
    </div> 
    <% end %> 
    </div> 


    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

耙路線:

 test_suite_new GET /test_suite/new(.:format)       test_suite#new 
    test_suite_tests GET /test_suites/:test_suite_id/tests(.:format)   tests#index 
        POST /test_suites/:test_suite_id/tests(.:format)   tests#create 
new_test_suite_test GET /test_suites/:test_suite_id/tests/new(.:format)  tests#new 
edit_test_suite_test GET /test_suites/:test_suite_id/tests/:id/edit(.:format) tests#edit 
    test_suite_test GET /test_suites/:test_suite_id/tests/:id(.:format)  tests#show 
        PUT /test_suites/:test_suite_id/tests/:id(.:format)  tests#update 
        DELETE /test_suites/:test_suite_id/tests/:id(.:format)  tests#destroy 
     test_suites GET /test_suites(.:format)        test_suites#index 
        POST /test_suites(.:format)        test_suites#create 
     new_test_suite GET /test_suites/new(.:format)       test_suites#new 
    edit_test_suite GET /test_suites/:id/edit(.:format)      test_suites#edit 
      test_suite GET /test_suites/:id(.:format)       test_suites#show 
        PUT /test_suites/:id(.:format)       test_suites#update 
        DELETE /test_suites/:id(.:format)       test_suites#destroy 
       root  /             home#index 
+0

hi @vesselli,我不確定我是否完全遵循了你正在努力實現的目標,或許你可以在問題中添加對目標的快速描述。但是通過查看代碼,我不知道你是否會混淆嵌套資源和嵌套表單?看看http://railscasts.com/episodes/139-nested-resources/和http://railscasts.com/episodes/196-nested-model-form-part-1 –

回答

1

該錯誤是由這兩條線的DIS-一致性引起的:

resources :test_suites do

<%= form_for([:test_suite, @test], :action => "index") do |f| %>

採用resources(相對於resource)意味着集合(如反對單一資源)。嵌套在其中的資源不屬於集合,但它們必須屬於集合中的特定成員。

在你的form_for([:test_suite, @test])你說你想要一個屬於稱爲「test_suite」的單一資源的表單。

您或者需要將您的父資源更改爲單數,或者通過特定的test_suite實例(即:form_for([@a_test_suite_instance, @test]))。

+0

我結束瞭解我的問題,這似乎完全不相關。我的測試索引視圖有一些不正確的路徑,這就是我得到路由錯誤的原因。 – vesselll

相關問題