routes.rb
get 'students/name_starts_with/:letter', to: 'students#name_starts_with'
get 'students/with_last_name/:last', to: 'students#with_last_name'
students_controller.rb
def name_starts_with
@students = Student.all.select{|s| s.first_name.start_with?(params[:letter]}
render json: @students.to_json
end
def with_last_name
@students = Student.all.select{|s| s.last_name == params[:last]}
render json: @students.to_json
end
students_controller_spec.rb
context '#name_starts_with' do
let!(:first_student){Student.create(first_name: 'John', last_name: 'Doe'}
it "starts with #{first_student.first_name.split('').first}" do
get :name_starts_with
expect(response.status).to eq(200)
expect(first_student.first_name.split('').first).to be('J')
end
end
context '#with_last_name' do
let!(:first_student){Student.create(first_name: 'John', last_name: 'Doe'}
it "has last name #{first_student.last_name}" do
get :with_last_name
expect(response.status).to eq(200)
expect(first_student.last_name).to be('Doe')
end
end
I have seeded bunch of student names. As far as I know both of these should be get routes/get requests. I am getting same error for both:-
Failure/Error: get :name_starts_with
ActionController::UrlGenerationError:
No route matches {:action=>"name_starts_with", :controller=>"students"}
# ./spec/controllers/students_controller_spec.rb:45:in `block (3 levels) in <top (required)>'
Should they be POST routes instead. Am I missing something. Am I doing whole thing wrong. Can somebody please help me solve this issue please.
I think this is route matching error. parameter letter is missing in this call.
Replace
get :name_starts_with
with
get :name_starts_with, letter: first_student.first_name.split('').first
I think this will fix your error.