简体   繁体   English

link_to 助手崩溃

[英]Crashes on link_to helper

I'm reading the "Agile Web Development with Rails 6" book, and I'm facing an issue from the beginning exercice.我正在阅读“Agile Web Development with Rails 6”一书,我从一开始就面临一个问题。

The book says to generate a Say controller, with 2 methods: hello and goodbye.书中说要生成一个 Say controller,有两种方法:你好和再见。 And then, we're asked to link those 2 views by using the "link_to" helper.然后,我们被要求使用“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

When I run this, it crashes on both views with this error (and I get the very same error with the goodbye template):当我运行它时,它在两个视图上都崩溃并出现此错误(并且我得到与再见模板相同的错误):

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 '('

When I run rake routes :当我运行rake routes时:

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

Obviously the routes do exist so the say_xxxx_path should work.显然路由确实存在,所以 say_xxxx_path 应该可以工作。 I've another tutorial project such a pattern work as expected.我有另一个教程项目,这种模式按预期工作。 Both projects run on Rails 6.0.3.2 / Ruby 2.7.0.这两个项目都在 Rails 6.0.3.2 / Ruby 2.7.0 上运行。 The Gemfile of the 2 project is same. 2项目的Gemfile是一样的。

I can't figure out what's wrong in there.我无法弄清楚那里出了什么问题。 I suspect something should be wrong in this project Rails stack, but I don't know how to check this.我怀疑这个项目 Rails 堆栈中应该有问题,但我不知道如何检查。

Indeed there is an error in your links Try确实您的链接中有错误尝试

<%= link_to 'Goodbye', say_goodbye_path %>

Can you spot the difference?您看得出来差别吗? No. Well neither can I but when I copied and pasted your code to my app I got the same error you are seeing.不,我也不能,但是当我将您的代码复制并粘贴到我的应用程序时,我遇到了与您看到的相同的错误。

Copy and paste that and see if it helps复制并粘贴它,看看它是否有帮助

I suspect your editor may be including hidden control characters.我怀疑您的编辑器可能包含隐藏的控制字符。 What editor are you using?你用的是什么编辑器? I highly recommend Atom and developing under linux will make your life a whole lot simpler我强烈推荐 Atom 并在 linux 下开发将使您的生活变得更简单

If you don't get to the bottom of this you are going to have many hours of debugging problems that do not exist如果您没有深入了解这一点,您将有许多小时的调试问题,而这些问题并不存在

No one has answered your question because there is no visible problem with your code没有人回答您的问题,因为您的代码没有明显的问题

UPDATE更新

On further investigation deleting each character in turn and replacing it with the typed in code solved the problem, It seems that every space including the beginning of the line has a hidden controller character and I had to delete two characters to get rid of a single space so I can only conclude that your editor is seriously stuffed up在进一步调查中依次删除每个字符并用键入的代码替换它解决了问题,似乎包括行首在内的每个空格都有一个隐藏的 controller 字符,我不得不删除两个字符以摆脱一个空格所以我只能得出结论,你的编辑器被严重塞满了

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

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