How to access URL in Rails Show action in Controller

This is the URL I want to access in my show method

http://localhost:3000/students.1

but the error is coming

ActiveRecord::RecordNotFound in StudentsController#show Couldn't find Student with 'id'=

def show

@student = Student.find(params[:id])

end

my routes are

new_students GET /students/new(.:format) students#new

edit_students GET /students/edit(.:format) students#edit

 students GET    /students(.:format)      students#show

          PATCH  /students(.:format)      students#update

          PUT    /students(.:format)      students#update

          DELETE /students(.:format)      students#destroy

          POST   /students(.:format)      students#create

     root GET    /                        students#index

Can someone tell me how do I access that students.1 in my URL so that I can display that particular student on my show page ?

Upvotes: 0

Views: 2219

Answers (3)

max
max

Reputation: 102443

Thats the wrong path. It should be /students/1.

And your routes are wrong. You can tell that the routes are off by the lack of an id segment and the fact that it maps GET /students to the show action instead of the index.

# Wrong
resource :students

# Right
resources :students

Pluralization is extremely important in Rails and is worth paying careful attention to. While its just one tiny letter these methods produce completely different routes.

resource is for singular resources. This is not the case here.

Upvotes: 1

Yury Matusevich
Yury Matusevich

Reputation: 998

It should be http://localhost:3000/students/1

Check out this docs

Upvotes: 0

Snkt
Snkt

Reputation: 1

there is some issue with your route formation

your url is not properly forming , try below

students_path(id: student.id)

Upvotes: 0

Related Questions