ruby - Rails 3 - Routing error when submitting forms using nested resources -
i'm new rails , i'm having lot of trouble getting nested resource/models work. i'm getting routing error whenever try submit form.
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>} can explain me why there occurrences of test_suite_id in exception output , why action "show" instead of "index"? when submit form, want go index /test_suites/:test_suite_id/tests.
below relevant code:
routes.rb
resources :test_suites resources :tests end tests_controller.rb
class testscontroller < applicationcontroller # /tests # /tests.json def index @testsuite = testsuite.find(params[:test_suite_id]) @tests = @testsuite.tests respond_to |format| format.html # index.html.erb format.json { render json: @tests } end end # /tests/1 # /tests/1.json def show @testsuite = testsuite.find(params[:test_suite_id]) @test = @testsuite.tests.find(params[:id]) respond_to |format| format.html # show.html.erb format.json { render json: @test } end end # /tests/new # /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 |format| format.html # new.html.erb format.json { render json: @test } end end # /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 |format| if @test.save flash[:notice] = "test added succesfully" format.html { redirect_to [:test_suite, @test], notice: 'test 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") |f| %> <% if @test.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@test.errors.count, "error") %> prohibited test being saved:</h2> <ul> <% @test.errors.full_messages.each |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 %> rake routes:
test_suite_new /test_suite/new(.:format) test_suite#new test_suite_tests /test_suites/:test_suite_id/tests(.:format) tests#index post /test_suites/:test_suite_id/tests(.:format) tests#create new_test_suite_test /test_suites/:test_suite_id/tests/new(.:format) tests#new edit_test_suite_test /test_suites/:test_suite_id/tests/:id/edit(.:format) tests#edit test_suite_test /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 /test_suites(.:format) test_suites#index post /test_suites(.:format) test_suites#create new_test_suite /test_suites/new(.:format) test_suites#new edit_test_suite /test_suites/:id/edit(.:format) test_suites#edit test_suite /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
the error caused dis-congruity of these 2 lines:
resources :test_suites do
<%= form_for([:test_suite, @test], :action => "index") |f| %>
the use of resources (as opposed resource) implies collection (as opposed singular resource). resources nested within not belong collection, though, must belong specific member in collection.
in form_for([:test_suite, @test]) saying want form belongs singular resource called "test_suite".
you either need change parent resource singular, or pass in specific test_suite instance (ie: form_for([@a_test_suite_instance, @test])).
Comments
Post a Comment