ruby-on-rails - 如何在 Ruby on Rails 中链接到另一个 .html.erb 文件

标签 ruby-on-rails

我正在处理我的 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.erbmy 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/

相关文章:

ruby-on-rails - 微博评论用户页(Ruby on Rails)

ruby-on-rails - ActiveRecord::AssociationTypeMismatch:用户预期,得到 Fixnum

ruby-on-rails - 使用日期和时间

ruby-on-rails - 在属性 setter 可以类型转换之前进行验证

ruby-on-rails - 使用 Devise 4 登录后重定向

ruby-on-rails - 在我的index.html.erb中为nil:NilClass获取未定义的方法 `each'

ruby-on-rails - Cucumber:Factory Girl 不修改数据库——使用 .create 创建对象但之后数据库表仍然为空

ruby-on-rails - 无法理解 Rails 中符号的使用

javascript - 如果没有选中复选框,则禁用按钮 jquery

mysql - 将 select 和collect 方法转换为联接