Question

I am trying to solve the following error:

ElementsControllerTest#test_should_get_create:
ActionController::UrlGenerationError: No route matches {:action=>"create", :controller=>"elements"}
test/controllers/elements_controller_test.rb:15:in `block in <class:ElementsControllerTest>'

Element is a nested resource (child of Piece)

    resources :batiments do
      resources :pieces
    end

    resources :pieces, shallow: true do
      resources :elements
    end

test/controllers/elements_controller.rb

class ElementsController < ApplicationController
 #after_filter :load_piece

  def index
   @piece = Piece.find(params[:piece_id])
    @elements = @piece.elements.all
  end

  def new
    @piece = Piece.find(params[:piece_id])
    @element = @piece.elements.new
  end


 def create
   @piece = Piece.find(params[:piece_id])
   @element = @piece.elements.new(element_params)
   if @element.save
     flash[:notice] = "Elément créé!"
   else
     flash[:alert] = "Elément non créé!"
   end
   redirect_to piece_path(@piece)
 end

…

end

and my routes for Element:

            Prefix Verb   URI Pattern                                       Controller#Action

     piece_elements GET    /pieces/:piece_id/elements(.:format)              elements#index
                    POST   /pieces/:piece_id/elements(.:format)              elements#create
  new_piece_element GET    /pieces/:piece_id/elements/new(.:format)          elements#new
       edit_element GET    /elements/:id/edit(.:format)                      elements#edit
            element GET    /elements/:id(.:format)                           elements#show
                    PATCH  /elements/:id(.:format)                           elements#update
                    PUT    /elements/:id(.:format)                           elements#update
                    DELETE /elements/:id(.:format)                           elements#destroy
             pieces GET    /pieces(.:format)                                 pieces#index
                    POST   /pieces(.:format)                                 pieces#create
          new_piece GET    /pieces/new(.:format)                             pieces#new
         edit_piece GET    /pieces/:id/edit(.:format)                        pieces#edit
              piece GET    /pieces/:id(.:format)                             pieces#show
                    PATCH  /pieces/:id(.:format)                             pieces#update
                    PUT    /pieces/:id(.:format)                             pieces#update
                    DELETE /pieces/:id(.:format)                             pieces#destroy

UPDATE: elements_controller_test.rb in /test

     require 'test_helper'

    class ElementsControllerTest < ActionController::TestCase
      test "should get create" do
        get :create
        assert_response :success
      end
    end
Was it helpful?

Solution

Change your test case as below:

  class ElementsControllerTest < ActionController::TestCase
      test "should get create" do
        post :create, element: {attrb1: "value", attrb2: "value"}, piece_id: id_of_existing_piece_record                    ## Post method
        assert_response :success
      end
    end

create is a post route not a get route. This is why you are getting error as No route matches {:action=>"create", :controller=>"elements"} Also, as elements is nested within pieces , you would have to pass an id of existing piece to your post :create call.

element: {attrb1: "value", attrb2: "value"} is for the attributes that you would like to save in element record.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top