0

I have defined a singular resource in my routes.rb which looks like this

Rails.application.routes.draw do
  resource :dog, only: [:create], to: "dog#create", controller: "dog"
end

After that I've defined a controller with a create action like this

class DogController < ApplicationController
  def create
    render json: {}, status: :ok
  end
end

And now I'm trying to test it out with RSpec like this

require "rails_helper"

describe DogController do
  it "works" do
    post :create, params: { foo: :bar }
  end
end

This is throwing this error instead of passing:

ActionController::UrlGenerationError:
       No route matches {:action=>"create", :controller=>"dog", :foo=>:bar}

What am I doing wrong?

2 Answers 2

1

Change your route to

resource :dog, only: [:create], :controller => "dog"

It is better to use plural controllers even if its a singular resource

http://edgeguides.rubyonrails.org/routing.html#singular-resources

Sign up to request clarification or add additional context in comments.

2 Comments

it's passing if I change controller name to DogsController, but I would really like to keep it singular and I would hate to change names of my controllers just to appease testing framework
removing to: argument makes it work as a singular, so I edited your question
0

Your create action is not taking in any parameter. It's just rendering json and returning a status code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.