繁体   English   中英

link_to 助手崩溃

[英]Crashes on link_to helper

我正在阅读“Agile Web Development with Rails 6”一书,我从一开始就面临一个问题。

书中说要生成一个 Say controller,有两种方法:你好和再见。 然后,我们被要求使用“link_to”帮助器来链接这两个视图。

<h1>Goodbye!</h1>
<p>
​ <%=​ link_to ​'Hello', say_hello_path ​%>​
</p>

<h1>Hello</h1>
​<p>
 <%=​ link_to ​'Goodbye'​, say_goodbye_path ​%>​
</p>
class SayController < ApplicationController
  def hello
    @time = Time.now
  end

  def goodbye
  end
end
Rails.application.routes.draw do
  get 'say/hello'
  get 'say/goodbye'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

当我运行它时,它在两个视图上都崩溃并出现此错误(并且我得到与再见模板相同的错误):

app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting `do' or '{' or '('
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting ')'
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting `do' or '{' or '('
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting ')'
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting `do' or '{' or '('
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting ')'
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting `do' or '{' or '('
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting ')'
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting `do' or '{' or '('
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting ')'
app/views/say/hello.html.erb:3: syntax error, unexpected local variable or method, expecting `do' or '{' or '('

当我运行rake routes时:

say_hello GET /say/hello(.:format) say#hello
say_goodbye GET /say/goodbye(.:format) say#goodbye

显然路由确实存在,所以 say_xxxx_path 应该可以工作。 我有另一个教程项目,这种模式按预期工作。 这两个项目都在 Rails 6.0.3.2 / Ruby 2.7.0 上运行。 2项目的Gemfile是一样的。

我无法弄清楚那里出了什么问题。 我怀疑这个项目 Rails 堆栈中应该有问题,但我不知道如何检查。

确实您的链接中有错误尝试

<%= link_to 'Goodbye', say_goodbye_path %>

您看得出来差别吗? 不,我也不能,但是当我将您的代码复制并粘贴到我的应用程序时,我遇到了与您看到的相同的错误。

复制并粘贴它,看看它是否有帮助

我怀疑您的编辑器可能包含隐藏的控制字符。 你用的是什么编辑器? 我强烈推荐 Atom 并在 linux 下开发将使您的生活变得更简单

如果您没有深入了解这一点,您将有许多小时的调试问题,而这些问题并不存在

没有人回答您的问题,因为您的代码没有明显的问题

更新

在进一步调查中依次删除每个字符并用键入的代码替换它解决了问题,似乎包括行首在内的每个空格都有一个隐藏的 controller 字符,我不得不删除两个字符以摆脱一个空格所以我只能得出结论,你的编辑器被严重塞满了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM