Home > Blockchain >  Syntax error when adding params to link_to helper
Syntax error when adding params to link_to helper

Time:12-31

I get an "syntax error, unexpected `end', expecting end-of-input" when trying to add params to link_to

html

<%= link_to  "showdeals"(id: "test"), target: :_blank  do %>
                <img src="/assets/images/1.png">
<% end%>

the following code without params works fine

<%= link_to  "showdeals", target: :_blank  do %>
                <img src="/assets/images/1.png">
<% end%>

CodePudding user response:

The second parameter should be the URL you would like to link. Try using the link

<%= link_to "showdeals", "my/link/goes/here", target: :_blank  do %>
   <img src="/assets/images/1.png">
<% end%>

Or ideally, you would have a resourceful link helper:

<%= link_to "showdeals", model_name_path(id: 1), target: :_blank  do %>
   <img src="/assets/images/1.png">
<% end%>

CodePudding user response:

This has nothing to do with link_to or even Rails.

In Ruby its syntactically invalid to follow a literal (string, array, hash) with (.

irb(main):001:0> "foo"(bar: 2)
/home/max/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/irb-1.4.2/lib/irb/workspace.rb:119:in `eval': (irb):1: syntax error, unexpected '(', expecting end-of-input (SyntaxError)
"foo"(bar: 2)
 ^

Perhaps you meant to call a path helper:

<%= link_to  "showdeals", deal_path(id: "test"), target: :_blank  do %>
  <img src="/assets/images/1.png">
<% end %>

Or pass a hash to the polymorphic routes helpers:

<%= link_to  "showdeals", { action: :show, id: "test" }, target: :_blank  do %>
  <img src="/assets/images/1.png">
<% end %>
  • Related