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