我对 Rails 完全陌生,在让我的嵌套资源/模型工作时遇到了很多麻烦。每当我尝试提交表单时,都会遇到路由错误。
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 以及为什么操作是“show”而不是“index”?当我提交表单时,我想回到索引 /test_suites/:test_suite_id/tests。
以下是所有相关代码:
路线.rb
resources :test_suites do
resources :tests
end
测试控制器.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