私はレールにまったく慣れていないので、ネストされたリソース/モデルを機能させるのに多くの問題を抱えています。フォームを送信しようとすると、ルーティング エラーが発生します。
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」ではなく「show」である理由を誰かが説明できますか? フォームを送信したら、インデックス /test_suites/:test_suite_id/tests に戻りたいと思います。
以下は、関連するすべてのコードです。
ルート.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