Rails: link_to do 块不起作用
Rails: link_to with do block doesn't work
当 'link_to' 没有阻塞地使用时,它完美地工作:
<%= link_to "Ololo", {controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link" %>
但是当我尝试使用 block 时,它以错误告终:
<%= link_to {controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link" do %>
<%= image_tag('icons/candy.svg', alt: "Candies", class:"rounded-circle icon-nav") %>
<%= post.get_upvotes.size %>
<% end %>
这是错误消息:
SyntaxError (/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ':', expecting '}'
...r.append= link_to {controller: controller_name, action: "up...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ',', expecting '}'
...troller_name, action: "upvote", id: entity.id}, method: :put...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected tLABEL
...pvote", id: entity.id}, method: :put, class:"nav-link", remo...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ',', expecting keyword_end
...method: :put, class:"nav-link", remote: true do @output_buff...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:49: syntax error, unexpected keyword_ensure, expecting end-of-input):
所以它似乎不理解文字散列,但是,我太新手了,无法弄清楚哪里出了问题...感谢任何帮助!
P.S.: 我无法删除文字散列,因为 link_to 与 css class 混淆(添加 class 作为一部分地址)。此外,我必须使用较旧的参数样式而不是 'upvote_post_path' 之类的东西,因为控制器名称由变量表示(在我的示例中,为了代码的可读性,它被减少了)
更新:
routes.rb
Rails.application.routes.draw do
root to: "home#index"
resources :posts do
member do
put "upvote", to: "posts#upvote"
end
end
end
左大括号:
some_method { a: 'b' }
是模棱两可的。 {
可以像 a.each { ... }
那样打开一个块,也可以打开一个散列文字。你认为是后者,但 Ruby 认为是前者,这就是错误的来源。
最简单的解决方案是使用方法调用括号:
<%= link_to({controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link") do %>
^--------------------------------------------------------------------------------------------------^
您还可以使用变量将左大括号移动到别处:
<% upvote_post = { controller: 'posts', action: 'upvote', id: post.id } %>
<%= link_to upvote_post, method: :put, remote: true, class:"nav-link" do %>
这个:
link_to "Ololo", {controller: "posts", ...
工作正常,因为第一个参数 ("Ololo",
) 通过告诉 Ruby 它正在解析参数列表来消除任何歧义。
当 'link_to' 没有阻塞地使用时,它完美地工作:
<%= link_to "Ololo", {controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link" %>
但是当我尝试使用 block 时,它以错误告终:
<%= link_to {controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link" do %>
<%= image_tag('icons/candy.svg', alt: "Candies", class:"rounded-circle icon-nav") %>
<%= post.get_upvotes.size %>
<% end %>
这是错误消息:
SyntaxError (/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ':', expecting '}'
...r.append= link_to {controller: controller_name, action: "up...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ',', expecting '}'
...troller_name, action: "upvote", id: entity.id}, method: :put...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected tLABEL
...pvote", id: entity.id}, method: :put, class:"nav-link", remo...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:5: syntax error, unexpected ',', expecting keyword_end
...method: :put, class:"nav-link", remote: true do @output_buff...
... ^
/home/alex/test/app/views/application/_votes_exp.html.erb:49: syntax error, unexpected keyword_ensure, expecting end-of-input):
所以它似乎不理解文字散列,但是,我太新手了,无法弄清楚哪里出了问题...感谢任何帮助!
P.S.: 我无法删除文字散列,因为 link_to 与 css class 混淆(添加 class 作为一部分地址)。此外,我必须使用较旧的参数样式而不是 'upvote_post_path' 之类的东西,因为控制器名称由变量表示(在我的示例中,为了代码的可读性,它被减少了)
更新:
routes.rb
Rails.application.routes.draw do
root to: "home#index"
resources :posts do
member do
put "upvote", to: "posts#upvote"
end
end
end
左大括号:
some_method { a: 'b' }
是模棱两可的。 {
可以像 a.each { ... }
那样打开一个块,也可以打开一个散列文字。你认为是后者,但 Ruby 认为是前者,这就是错误的来源。
最简单的解决方案是使用方法调用括号:
<%= link_to({controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link") do %>
^--------------------------------------------------------------------------------------------------^
您还可以使用变量将左大括号移动到别处:
<% upvote_post = { controller: 'posts', action: 'upvote', id: post.id } %>
<%= link_to upvote_post, method: :put, remote: true, class:"nav-link" do %>
这个:
link_to "Ololo", {controller: "posts", ...
工作正常,因为第一个参数 ("Ololo",
) 通过告诉 Ruby 它正在解析参数列表来消除任何歧义。