views:

41

answers:

3

The following code:

<div id="vote_form">
  <%= form_remote_tag :url => story_votes_path(@story) do %>
    <%= submit_tag 'shove it' %>
  <% end %>
</div>

gives compilation error

while if the first <%= is replaced with <%, then everything works. I thought they only differ by "show" or "not show", but why will it actually cause a compile error?

The error is:

> SyntaxError in Stories#show
> 
> Showing
> app/views/stories/show.html.erb where
> line #17 raised:
> 
> compile error C:/Software
> Projects/ror/shov12/app/views/stories/show.html.erb:17:
> syntax error, unexpected ')' ...
> story_votes_path(@story) do ).to_s);
> @output_buffer.concat ...
>                               ^ C:/Software
> Projects/ror/shov12/app/views/stories/show.html.erb:23:
> syntax error, unexpected kENSURE,
> expecting ')' C:/Software
> Projects/ror/shov12/app/views/stories/show.html.erb:25:
> syntax error, unexpected kEND,
> expecting ')'
+1  A: 

You see it in your error message. When using <%= ... %> erb will replace this with (...).to_s. Ruby gets confused when a do is followed by a closing paranthesis, it expects a block of some sort instead.

harald
A: 

This is actually an inconsistency in how Rails versions prior to 3 handled Ruby blocks within ERB templates. Because the form_for helper inserts an HTML form element into the view around the content it should have used an equals sign in its ERB tags and Rails 3 fixes this.

John Topley
+1  A: 

Take a look at this

when you are using form_tag it calls the form_tag method of FormHelper Class. This helper method return s the html depend on your code.

     # File vendor/rails/actionpack/lib/action_view/helpers/prototype_helper.rb, line 331
331:       def form_remote_tag(options = {}, &block)
332:         options[:form] = true
333: 
334:         options[:html] ||= {}
335:         options[:html][:onsubmit] =
336:           (options[:html][:onsubmit] ? options[:html][:onsubmit] + "; " : "") +
337:           "#{remote_function(options)}; return false;"
338: 
339:         form_tag(options[:html].delete(:action) || url_for(options[:url]), options[:html], &block)
340:       end

From this you will come to know that the Complier expecting block and it thows error when it didn't get it.

Salil