1

我遇到了一个我无法弄清楚的错误,因为它适用于另一种形式。我正在尝试向表单添加一个类 - 它看起来像这样:

<%= form_for([@post, @post.comments.build]), html: {class: 'form-horizontal'} do |f| %> 

但这会引发错误:

syntax error, unexpected tLABEL ..., @post.comments.build]), html: {class: 'form-horizontal'} d...

syntax error, unexpected keyword_do_block, expecting keyword_end

如果我删除 html: ... 表单有效(但看起来很糟糕)。

这个有效(出于某种原因): <%= form_for @post, html: {class: 'form-horizontal'} do |f| %>

它可能很容易修复,但由于我是编程新手 - 我只是不明白;)

提前致谢!

4

3 回答 3

1

您收到此错误的原因是因为您在帮助)者的错误位置有右括号form_for

请试试:

<%= form_for([@post, @post.comments.build], html: {class: 'form-horizontal'}) do |f| %>

另一种选择是在之后添加一个空格form_for作为参数([@post, @post.comments.build])html: {class: 'form-horizontal'}form_for

<%= form_for ([@post, @post.comments.build]), html: {class: 'form-horizontal'} do |f| %>
于 2013-08-11T21:40:05.357 回答
0

我认为您需要在 html 哈希之前包含选项哈希(即使它是空的):

<%= form_for([@post, @post.comments.build]), {}, {class: 'form-horizontal'} do |f| %>
于 2013-08-11T21:07:45.700 回答
0

我认为你真的很亲密,但我认为这可能有效:

<%= form_for([@post, @comments]), :html => {class: 'form-horizontal'} do |f| %>

我从这个问题中得到了form_for() 的多个参数

于 2013-08-11T21:11:07.507 回答