syntax error, Unexpected ',', expecting ')' - Rails 4

Asked

Viewed 131 times

0

The Rails interpreter is sending me this message in the following line of code:

...
<li>
  <%= form_tag("search", { method: "get", class: "navbar-form navbar-right", role: "search" }) do %>
    <div class="inner-addon right-addon">
      <i class="glyphicon glyphicon-search"></i>
      <%= text_field_tag (:q, nil , { class: "form-control", height: "25", width: "25" } ) %>
    </div>
  <% end %>
<li>
...

There must be something wrong with passing the form_tag or text_field_tag hash but I can’t identify it

  • 3

    as the error says, it is waiting to close the function with ) and you are including one more unexpected parameter... check which is the exact line of the error and review if all parameters are expected.

  • The error message undoubtedly includes the exact line number and character position of that line. Check this.

  • Got here, the line of error was this: <%= text_field_tag (:q, nil , { class: "form-control", height: "25", width: "25" } ) %> I switched to: <%= text_field_tag :q, nil, { class: "form-control", size: "25x25" } %> E worked ... I just don’t know why...

  • @Awkward Post the solution as a response and mark it as accepted

  • @Andrey I did not publish as an answer, because even solving the problem I did not know why. Guilherme did a great job explaining. Next time I put as an answer, it makes more sense anyway.

1 answer

4


The problem:

text_field_tag (:q, nil , { class: "form-control", height: "25", width: "25" } )

Simply put, you have the following:

funcao (1, 2, 3)

Notice the space before opening the parentheses. This is fundamental. Ruby allows you to pass arguments to a function if no parentheses are used, and at the same time allows you to use parentheses to group simple expressions such as (1+5)*2. What happens there is that because of this space he tries to read as if he had passed a single argument: the expression inside the parentheses. And there’s the problem, it’s not to have a comma in the middle of the expression. Correction:

funcao(1, 2, 3)

Or if you prefer:

funcao 1, 2, 3

Note, in the particular case of passing a Hash as the last argument of the function, you can omit the keys to improve readability. If you want, you can still write like this:

text_field_tag(:q, nil, class: "form-control", height: "25", width: "25")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.