0

I am receiving a syntax error, unexpected =>, expecting '}' in my the following view code:

<%= render :partial => 'form', :locals => { :message => @message, :user => @user} %>

I am rendering the partial from my new.html page. The partial looks right to my eyes but perhaps I am missing something?

_form.html.erb:

<%= form_for message, :url => user_messages_path(user), :html => {:class => "MainForm"} do |f| %>

  <%if message.reply_to%>
    <%=f.hidden_field :to%>
    <%=f.hidden_field :parent_id%>
  <%else%>
    <%label{:for => 'message_to'} %>
      <%=:to.l%>
      <%em= "(" + :type_a_username.l + ")"%>
    <%= text_field_tag 'message[to]', @message.to, {:autocomplete => "off", :size => 35, :id => "message_to"}%>
    <% auto_complete%>#message_to_auto_complete
    <%content_for :end_javascript do%>
      <%= auto_complete_field 'message_to', {:url => auto_complete_for_username_user_messages_path(@user), :tokens=>','}%>

  <%if @reply  %>
    <%= f.hidden_field :subject  %>
  <%else%>
    <%label{:for => 'message_subject'}= :subject.l + ":"%>
    <%= f.text_field :subject%>

  <%label{:for => "message_body"}= :message.l + ":"%>
  <%= f.text_area :body%>

  <%p%>
    <%= submit_tag :send.l%>
5
  • 2
    Could you post your whole view? Commented Feb 25, 2014 at 16:29
  • 1
    As Vimsha said, we'll need to see a bit more; the error might not be on that line. Alternatively (I doubt it will resolve the issue), if you're using Ruby... 2.0 I believe it is, you can try the new hash syntax (for example: render partial: 'form', locals: {message: @message, user: @user}) Commented Feb 25, 2014 at 16:34
  • @PaulRichter, while the JSON-style notation has come into vogue as of Ruby 2.0, I don't think the hash-rocket notation has been deprecated... yet. Commented Feb 25, 2014 at 16:37
  • @zeantsoi Nope, you're right its still perfectly valid. Twas merely a general suggestion. Might help to remove a bit of noise to more easily spot syntax errors and omissions. Commented Feb 25, 2014 at 16:38
  • @Vimsha I posted the form view. Commented Feb 25, 2014 at 16:40

2 Answers 2

2

The label tag does not contain a hash. From the docs:

<% f.label :message_to %>

The following line from your code looks particularly suspicious, and it certainly will throw an error. What are you trying to do with this exactly?

<%label{:for => 'message_subject'}= :subject.l + ":"%> # THIS WON'T WORK

If you want custom text for your label, you can accomplish it in the following manner:

<%= f.label :message_subject, "#{subject.l} :" %>

Which outputs the following markup:

<label for="message_subject">THE MESSAGE SUBJECT</label>
Sign up to request clarification or add additional context in comments.

Comments

1

This syntax here (and in similar places throughout your view):

<%label{:for => 'message_subject'}= :subject.l + ":"%>

is invalid.

A (generally) correct label element would be:

<%= f.label :subject %>

Remember a couple of things:

  • Symbols (which is what that :subject is), are a special kind of object that ultimately represents an immutable string; it doesn't have that .l method you're expecting
  • Assuming :subject is a field of message, ensure you're using the form builder object (the f variable in this case) so the form element is properly bound to the message object
  • Form elements are echoed to the page using the = syntax in the ERB tag. It should always look like <%= ... %> if you want your code to render (or <% ... %> to suppress).

1 Comment

Thank you for explaining. I noticed that was throwing the error and now I understand how it all went wrong. Great notes!

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.