You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

给Rails的link_to助手添加参数时出现语法错误求助

问题场景

给Rails的link_to助手添加参数时,触发语法错误:syntax error, unexpected 'end', expecting end-of-input

报错代码

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

可正常运行的无参数代码

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

错误原因

直接在字符串"showdeals"后加(id: "test")不符合Ruby语法,Rails无法识别这种传参方式。给路由传递参数必须使用命名路由辅助方法(即xxx_path或xxx_url格式)。

解决方法

  1. 在路由配置中给目标路由设置命名(使用as: :showdeals),示例:
# config/routes.rb
get '/your-target-path', to: 'your_controller#your_action', as: :showdeals
  1. 在视图中通过命名路由辅助方法传递参数:
<%= link_to showdeals_path(id: "test"), target: :_blank  do %>
                <img src="/assets/images/1.png">
              <% end%>

内容的提问来源于stack exchange,提问作者Iqbal Bakhsh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 22:35:51