Padagomez Padagomez - 1 year ago 35
Ruby Question

syntax error, unexpected '<', expecting ')'

I'm trying to build a form in Ruby on Rails. The problem is, it keeps giving me an error syntax error, unexpected '<', expecting ')' when I start the table itself.

Here's the lines above the form:

<% @employee = %>
<!-- <h4><%= employee.first_name #+ " " + employee.middle_name + " " employee.last_name %></h4></br> -->

Here's the start of the form:

<%= form_for :pr_add_ons_deductions, url: { action: "create" } do |f| %>

This is where it's saying it is coming from, but I don't see anything wrong with it.

Here's the form itself which is where the '<' comes from.

<table summary="Section from fields">
<th><%= f.label(:type_id) %></th>
<th><%= f.label(:description) %></th>
<th><%= f.label(:amount) %></th>
<th><%= f.label(:recurrence) %></th>
<th><%= f.label(:end_date) %></th>
<td><select name="resultant" onchange="setResultant()"> <!--TODO-->
<option value="Add">Add On</option>
<option value="Deduct">Deduction</option>
<td><%= select(:pr_add_ons_deductions, :type_id, PrAddOnsDeductionsType.select_options) %></td>
<td><%= f.text_area(:description, :size => '40x2')%></td>
<td><%= f.text_field(:amount) %></td>
<td><%= f.text_field(:recurrence) %></td>
<td><%= date_select(:pr_add_ons_deductions, :end_date, :value =>"%m/%d/%Y")) %></td>

<div class="form-buttons">
<%= submit_tag("Create Entry") %>

<% end %>

Here's the contents of the controller:

def entry
@employee_id = params[:id]
@entry = => params[:id])

def create
@payroll =

flash[:notice] = "Entry Created Successfully."!
redirect_to(:action => "entry")

Besides this there is a params method that is private in the controller:

def params
params.require(:pr_add_ons_deductions).permit(:type_id, :description, :amount, :recurrence, :end_date, :sbm_employee_id)

Here's the model I use:

class SbmEmployee < ActiveRecord::Base
self.table_name = "sbm_employees"

self.where("id = #{id}")

Answer Source

If you run a bit of your ERB through erubis -x (note: Rails uses Erubis rather than plain ERB) to see what Ruby it is transpiled to, you'll see something like this:

_buf = ''; @employee =  
 _buf << '<!-- <h4>'; _buf << ( employee.first_name #+ " " + employee.middle_name + " " employee.last_name ).to_s; _buf << '</h4></br> -->
'; _buf << ( form_for :pr_add_ons_deductions, url: { action: "create" } do |f| ).to_s; _buf << '

The second line is of interest here. Erubis has thrown your comment right into the generated code without caring or understanding its effect. That should tell you why you're seeing that particular error message.

There are some lessons here:

  1. Ruby comments in ERB don't work quite like comments in Ruby code. ERB processors don't really understand Ruby, they just sling bits of text around in an attempt to generate valid Ruby code.
  2. ERB doesn't understand HTML comments either, HTML comments are just more text to sling around.
  3. Don't comment-out code to disable it, use revision control and delete the code instead.

If you delete Ruby comment inside the HTML comment:

<!-- <h4><%= employee.first_name %></h4></br> -->

then things should start working again. Or better, delete the whole HTML comment since it it just in the way.