When 'link_to' is used without block it works perfectly:
<%= link_to "Ololo", {controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link" %>
But when I try to use block it ends up with error:
<%= 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 %>
And here is the error message:
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):
So it seems like it doesn't understand literal hash, however, I'm too newbie to figure out what's wrong... Any help is appreciated!
P.S.: I can't remove literal hash, because link_to is messing up with css class (adds class as a part of address). Also I have to use older argument style instead of something like 'upvote_post_path' because controller name is represented by variable (in my example it is reduced for readability of the code)
UPDATE:
routes.rb
Rails.application.routes.draw do
root to: "home#index"
resources :posts do
member do
put "upvote", to: "posts#upvote"
end
end
end
The opening brace in things like:
some_method { a: 'b' }
is ambiguous. The {
could be opening a block as in a.each { ... }
or it could be opening a hash literal. You think it is the latter but Ruby thinks it is the former and that's where the error comes from.
The easiest solution is to use method-calling-parentheses:
<%= link_to({controller: "posts", action: "upvote", id: post.id}, method: :put, remote: true, class:"nav-link") do %>
^--------------------------------------------------------------------------------------------------^
You could also use a variable to move the opening brace elsewhere:
<% upvote_post = { controller: 'posts', action: 'upvote', id: post.id } %>
<%= link_to upvote_post, method: :put, remote: true, class:"nav-link" do %>
This one:
link_to "Ololo", {controller: "posts", ...
works fine because the first argument ("Ololo",
) removes any ambiguity by telling Ruby that it is parsing an argument list.