Search code examples
ruby-on-railshttp-status-code-404ruby-on-rails-5rails-routing

How do I get my "show" page to render?


I have Rails 5 installed. I'm having trouble getting my show page to render. My rake routes has this

votes_show GET /votes/show(.:format) votes#show

Then in my app/controllers/votes_controller.rb file I have

  def show
    id = params[:id]
    # If there is no person selected based on the param, choose one randomly
    if !id.present?
      @person = Person.order("RANDOM()").limit(1).first
    else
      @person = Person.find(:id => params[:id])
    end

but when I go to http://localhost:3000/votes/1, I get a

No route matches [GET] "/votes/1"

error. I'm missing something real obvious. What is it?


Solution

  • votes_show GET /votes/show(.:format) votes#show
    

    this line is telling rails that "when you see a route that is /votes/show then go to the VotesController#show method."

    When you then type in /votes/1, rails is checking that against the routes and not finding a route that looks like that... it is specifically looking only for /votes/show

    As mentioned in the comments, you must add a line like:

    get '/votes/:id(.:format)', to: 'votes#show'
    

    or better yet, use resourceful routes like

    resources :votes