【问题标题】:Ruby on Rails Routing Error for a link to a website网站链接的 Ruby on Rails 路由错误
【发布时间】:2016-10-23 18:13:48
【问题描述】:

我正在学习 Michael Hartl 的 Ruby on Rails 教程。 我的 app/views/static_pages/help.html.erb 包含指向网站 www.railstutorial.org 的链接 这是代码

<!DOCTYPE html>
<html>
  <head>
    <title>Help | Ruby on Rails Tutorial Sample App</title>
  </head>
  <body>
    <h1>Help</h1>
  <p>
    Get help on the Ruby on Rails Tutorial at the 
    <a href=“http://www.railstutorial.org/#help”>Rails Tutorial help section</a>
  </p>

但我收到错误 No route matches [GET] "/static_pages/%E2%80%9Chttp:/www.railstutorial.org"

routes.rb 中的 get 函数如何用于链接?是不是应该像

get 'static_pages/wwww.railstutorial.org' ?

【问题讨论】:

  • 您是否尝试过使用嵌入式 ruby​​ 作为链接?顺便说一句,您不需要在您的 routes.rb 文件中指定外部链接。
  • 您使用的是“智能引号”(),而不是普通的双引号(")。 HTML 使用普通的旧 ASCII 引号作为属性。
  • 嗨 Mint 也许你不想要一个链接到railstutorial.org/#help.?

标签: ruby-on-rails ruby railstutorial.org


【解决方案1】:

您需要一个名为StaticPagesController 的控制器,其中包含一个方法。那么名字就无所谓了。然后在路由文件中它将是get "static_pages/wwww.railstutorial.org", to: "static_pages#method_name" 为此,控制器看起来像:

class StaticPagesController < ActionController
  def method_name
  end
end

另请注意,您需要在名为 method_name 的 static_pages 视图文件夹中的相应视图

【讨论】:

  • 不,正如@mu在cmets中提到的那样太短,错误是使用“http://www.railstutorial.org/#help”智能引号,而不是标准的"http://www.railstutorial.org/#help"引号。无需其他控制器
【解决方案2】:

您根本不需要任何路由配置。这是一个简单的 HTML a 标签。在http://www.w3schools.com/tags/tag_a.asp中阅读有关 HTML a 标签的更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多