我正在处理我的 Rails 项目,但收到此错误:
ActiveRecord::RecordNotFound in LeaguesController#show Couldn't find League with 'id'=about
这是我的link_to
:
<%= link_to 'About', about_path %>
我的 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
还有我的routes.rb
:
Rails.application.routes.draw do
resources :leagues
get '/leagues/about' => 'leagues#about', as: => :about
end
请帮助我,我想链接我的index.html.erb
至my about.html.erb
.
最佳答案
resources :leagues
生成路线
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
然后,您要添加的自定义路由与为 leagues#show
操作委托(delegate)的路由具有相同的 URL 蓝图(即 /leagues/:id
),rails 路由器假定 :id
为“about”。您可以使用 match
来解决这个问题:
match '/leagues/about' => 'leagues#about', as: :about, via: :get
关于ruby-on-rails - 如何在 Ruby on Rails 中链接到另一个 .html.erb 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45258802/