簡體   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