2

I am trying to style form elements within this rails form_for but continually run into syntax error, nothing i try seems to be working, can you see the problem?

<%= form_for(@todo_list) do |f| %>
  <div class="form-group">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="form-group">
    <%= f.label :description %><br>
    <%= f.text_area :description %>
  </div>
  <div>
    <%= f.submit, html: {class: "btn btn-default btn-xs"} %>
  </div>
<% end %>

Results in this error: _form.html.erb:23: syntax error, unexpected tLABEL, expecting '=' ...buffer.append=( f.submit, html: {class: "btn btn-default btn... ...

All sorts of variations of class=, class:, html=>, etc etc just seem to give errors too.

<%= f.submit, class: "btn btn-default btn-xs" %>

results in this error:

SyntaxError in TodoListsController#edit ...... _form.html.erb:23: syntax error,unexpected tLABEL, expecting '=' ...uffer.append=( f.submit, class: "btn btn-default btn-xs" );@... ... ^

Passing 'form-horizontal' into form_for still produces an error;

<%= form_for(@todo_list, html => {:class => "form-horizontal"})  do |f| %>
....
....
<%= f.submit, class: "btn btn-default btn-xs" %>

SyntaxError in TodoListsController#edit ... _form.html.erb:23: syntax error, unexpected tLABEL, expecting '=' ...uffer.append=( f.submit, class: "btn btn-default btn-xs" );@... ... ^

Note that if i explicitly declare a label to the submit action, e.g.;

<%= f.submit 'Save', class: "btn btn-default btn-xs" %>

Then the class gets added successfully and my bootstrap styling works. However this neither locates the primary problem nor solves it as then I can't use rails' form_for dynamic naming of the submit action.

Classes I apply to divs seem to work fine, bootstrap is loaded fine. Can anyone help? Thanks

jbk
  • 1,911
  • 19
  • 36
  • You have an extra comma: `f.submit class: "btn btn-default btn-xs"` (don't use a comma after the `f.submit` call) (or eventually give `nil` as first argument) – MrYoshiji Oct 20 '15 at 13:58
  • @MrYoshiji please submit your comment as an answer, because it's the right one. Oh my, after hours and hours researching this, it was the tiniest thing!! Thank you. – jbk Oct 21 '15 at 15:04

4 Answers4

3

This is a typo error: you have an extra comma in:

f.submit, class: "btn btn-default btn-xs"
#       ^ extra comma

You should use the following:

f.submit nil, class: 'btn btn-default btn-xs'
#       ^ no comma
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
2

You forgot to add label for submit button

<%= f.submit 'Ok', class: "btn btn-default btn-xs" %>

EDIT Another way is rewrite default value of button:

<%= f.submit class: "btn btn-default btn-xs", value: 'Ok!' %>

and, as I think, here's what you're looking for:

<%= f.submit class: "btn btn-default btn-xs" %>

no comma after f.submit

Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
  • Without the attempted class addition the form works just fine without a submit action label? But I will try with a label now. – jbk Oct 20 '15 at 11:54
  • 1
    Add label and restart server – Igor Ivancha Oct 20 '15 at 11:57
  • Thanks Igor, this is working now. Can you help me understand why without additional classes and without specifically declared label things work fine but as soon as you add classes you need to declare a label? Thanks – jbk Oct 20 '15 at 12:00
  • I was glad to help you, `jbk`! http://apidock.com/rails/ActionView/Helpers/FormBuilder/submit – Igor Ivancha Oct 20 '15 at 12:11
  • Igor, thanks and happy to get out of that hole, but i now realise that previously rails form_for was automatically naming that submit button based upon the action being performed, not it's not and is just being overridden for all form usages with my declared label. So this kind of solved one problem but created another, how can i retain the dynamic label naming, but whilst also being able to apply custom classes to the element do you know? – jbk Oct 20 '15 at 12:18
  • yes, you can usually do without a label! maybe you had issue because bootstrap?)) – Igor Ivancha Oct 20 '15 at 12:19
  • So though your suggestion helped in one sense it hasn't diagnosed the specific issue here. I'll read the APIdoc stuff now. Any other ideas as to what's going on? – jbk Oct 20 '15 at 12:23
  • Sorry, I still know very little about the `ruby on rails`! You could make another way: `<%= f.submit class: "btn btn-default btn-xs", value: 'Ok!' %>` to rewrite default value generated button – Igor Ivancha Oct 20 '15 at 12:29
  • Thanks Igor, but I need to get the class addition working without overwriting the automatic button naming facility of form_for. – jbk Oct 20 '15 at 12:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92867/discussion-between-jbk-and-igor-ivancha). – jbk Oct 20 '15 at 13:10
0

Use like this.

<%= form_for(@todo_list, :html => {:class => "form-horizontal"}) do |f| %>

<%= f.submit class: "btn btn-default btn-xs"%>
Umar Khan
  • 1,381
  • 11
  • 18
  • Doesn't work unfortunately, added the specific error mssg in the main question body. – jbk Oct 20 '15 at 11:37
  • Ah, so i have to pass in such an html reference at the beginning of the form_for? What is the `bootstrap form` referring to? (doesn't work just as you wrote above.) – jbk Oct 20 '15 at 11:40
  • It will apply the bootstrap form class to your form. I am editing my above answer. You can apply that class and see the change :) – Umar Khan Oct 20 '15 at 11:43
  • So strange, still not working!?!; `_form.html.erb:23: syntax error, unexpected tLABEL, expecting '=' ...uffer.append=( f.submit, class: "btn btn-default btn-xs" );@... ... ^` – jbk Oct 20 '15 at 11:46
  • Have you added these changes? <%= form_for(@todo_list, html => {:class => "form-horizontal"}) do |f| %> – Umar Khan Oct 20 '15 at 11:55
  • Thanks Umar, I have, see the bottom of main question body for code and returned error. – jbk Oct 20 '15 at 11:57
  • Ahh, use : before html – Umar Khan Oct 20 '15 at 11:59
  • Afraid not Umar, still doesn't work. It seems that I needed to specifically declare a label to the submit action. See other answer on this post. – jbk Oct 20 '15 at 12:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92868/discussion-between-jbk-and-umar). – jbk Oct 20 '15 at 13:11
0

As mentioned by Umar, your f.submit classreference is incorrect:

<%= f.submit, class: "btn btn-default btn-xs" %>

You'll also benefit from using a loop in your form:

#form
<% options = [[:title, "text_field"],[:description, "text_area"]] %>
<%= form_for @todo_list do |f| %>
   <% options.each do |option| %>
      <%= content_tag :div, class: "form-group" %>
          <%= f.label option[0] %>
          <%= f.send(option[1], option[0]) %>
      <% end %>
   <% end %>
   <%= f.submit %>
<% end %>
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147