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

Rails项目products/_form.html.erb文件语法错误无法定位原因求助

Rails表单语法错误修复方案

你代码中存在两处语法错误:

  • 第3行报错位置的f.object.errors [:base]写法错误:Ruby中调用方法后直接跟数组索引时不允许加空格,errors和[:base]之间的空格会被解释器识别为方法调用结束,后续的[不符合预期语法触发报错,需要把空格删除,改为f.object.errors[:base],该行两处该写法都要同步修改。
  • 第9行的分类下拉配置语法错误:f.input的collection参数属于哈希配置项,符号键和值之间需要添加哈希连接符,你当前:collection和数组之间缺少连接符,需要改为collection: ["footwear", "accessories", "menswear", "womenswear"]或者:collection => ["footwear", "accessories", "menswear", "womenswear"]。

修复后完整代码

<%= simple_form_for(@product) do |f| %>
  <%= f.error :base %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <%= f.input :title %>
    <%= f.input :description %>
    <%= f.input :price %>
    <%= f.input :category, collection: ["footwear", "accessories", "menswear", "womenswear"] %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

内容的提问来源于stack exchange,提问作者Sneha Bhamra

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 11:12:04