Padagomez Padagomez - 7 months ago 10
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 = SbmEmployee.search(@employee_id).first %>
<!-- <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">
<tr>
<th>Resultant</th>
<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>
</tr>
<tr>
<td><select name="resultant" onchange="setResultant()"> <!--TODO-->
<option value="Add">Add On</option>
<option value="Deduct">Deduction</option>
</td>
<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 => Time.now.strftime("%m/%d/%Y")) %></td>
</tr>
</table>

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

<% end %>


Here's the contents of the controller:

def entry
@employee_id = params[:id]
@entry = PrAddOnsDeduction.new(:sbm_employee_id => params[:id])
end

def create
@payroll = PrAddOnsDeduction.new(params)

if @payroll.save
flash[:notice] = "Entry Created Successfully."
@payroll.save!
redirect_to(:action => "entry")
else
render("entry")
end
end


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)
end


Here's the model I use:

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

def self.search(id)
self.where("id = #{id}")
end
end

Answer

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 = SbmEmployee.search(@employee_id).first  
 _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.

Comments