1

I am running into an error I cant figure out, because it works with another form. I am trying to add a class to a form - it looks like this:

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

but this throws an error:

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

syntax error, unexpected keyword_do_block, expecting keyword_end

If I delete the html: ... the form works (but looks shitty).

This one works (for some reason): <%= form_for @post, html: {class: 'form-horizontal'} do |f| %>

It is probably easy to fix, but since I am new to programming - I just dont get it ;)

Thanks in advance!

3 Answers 3

1

The reason you are getting this error is because you have closing parenthesis ) at the wrong place for the form_for helper.

Please try:

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

Another option is to add a space after form_for so that ([@post, @post.comments.build]) and html: {class: 'form-horizontal'} become parameters to form_for

<%= form_for ([@post, @post.comments.build]), html: {class: 'form-horizontal'} do |f| %>
Sign up to request clarification or add additional context in comments.

Comments

0

I think you need to include the options hash (even if it is empty) before the html hash:

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

1 Comment

Hey Matt - thanks for the quick answer, but I am afraid that didnt do the trick :(. I have edited the post - maybe it helps.
0

I think you are really close, but i think this might work:

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

I got that from this question Multiple parameters for form_for()

1 Comment

Thanks TMP for the answer - but both solutions dont work. I added something to the post above - maybe that helps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.