Tatamatugas
Tatamatugas

Reputation: 165

How to link to another .html.erb file in Ruby on Rails

I am working with my Rails project and I am getting this error:

ActiveRecord::RecordNotFound in LeaguesController#show Couldn't find League with 'id'=about

This is my link_to:

<%= link_to 'About', about_path %>

My controller:

class LeaguesController < ApplicationController
  before_action :set_league, only: [:show, :edit, :update, :destroy]

  # GET /leagues
  # GET /leagues.json
  def index
    @leagues = League.all
  end

  def about
  end

  # GET /leagues/1
  # GET /leagues/1.json
  def show
  end

  # GET /leagues/new
  def new
    @league = League.new
  end
end

And my routes.rb:

Rails.application.routes.draw do
    resources :leagues
    get '/leagues/about' => 'leagues#about', as: => :about
end

Please help me, I want to link my index.html.erb to my about.html.erb.

Upvotes: 2

Views: 1567

Answers (2)

Simple Lime
Simple Lime

Reputation: 11035

In the rails routing guide This exact situation is covered:

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first

Instead of :photos you have :leagues and instead of /poll you have /about. So, just need to swap those 2 lines in your routing file

Rails.application.routes.draw do
    get '/leagues/about' => 'leagues#about', as: :about
    resources :leagues
end

For this situation, you could also do

resources :leagues do
  get 'about', on: :collection
end

and end up with

Prefix        Verb    URI Pattern                 Controller#Action
about_leagues GET    /leagues/about(.:format)    leagues#about

and end up with about_leagues_path as the helper (which might be better than about_path, about what? What if you have an /teams/about as well, later on, is about_path about teams or leagues?)

Upvotes: 1

sa77
sa77

Reputation: 3603

resources :leagues generates the routes

          leagues GET    /leagues(.:format)                        leagues#index
                  POST   /leagues(.:format)                        leagues#create
       new_league GET    /leagues/new(.:format)                    leagues#new
      edit_league GET    /leagues/:id/edit(.:format)               leagues#edit
           league GET    /leagues/:id(.:format)                    leagues#show
                  PATCH  /leagues/:id(.:format)                    leagues#update
                  PUT    /leagues/:id(.:format)                    leagues#update
                  DELETE /leagues/:id(.:format)                    leagues#destroy

Then, the custom route you want to add has the same URL blueprint as that of route delegated for leagues#show action (i.e /leagues/:id ), which the rails router assumes :id to be 'about'. You can use match to solve this:

match '/leagues/about' => 'leagues#about', as: :about, via: :get

Upvotes: 1

Related Questions