简体   繁体   中英

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.

The book says to generate a Say controller, with 2 methods: hello and goodbye. And then, we're asked to link those 2 views by using the "link_to" helper.

<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 :

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. I've another tutorial project such a pattern work as expected. Both projects run on Rails 6.0.3.2 / Ruby 2.7.0. The Gemfile of the 2 project is same.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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