[英]Syntax error, unexpected '}', expecting :: or '[' or '.'
[英]syntax error, unexpected '<', expecting ')'
我正在尝试在Ruby on Rails中构建表单。 问题是,当我启动表本身时,它一直给我一个错误语法错误,意外的<<,期望的是')'。
这是表格上方的行:
<% @employee = SbmEmployee.search(@employee_id).first %>
<!-- <h4><%= employee.first_name #+ " " + employee.middle_name + " " employee.last_name %></h4></br> -->
这是表格的开始:
<%= form_for :pr_add_ons_deductions, url: { action: "create" } do |f| %>
这就是它的来源,但我认为它没有任何问题。
这是表单本身,它是“ <”的来源。
<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 %>
这是控制器的内容:
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
除此之外,还有一个控制器中私有的params方法:
def params
params.require(:pr_add_ons_deductions).permit(:type_id, :description, :amount, :recurrence, :end_date, :sbm_employee_id)
end
这是我使用的模型:
class SbmEmployee < ActiveRecord::Base
self.table_name = "sbm_employees"
def self.search(id)
self.where("id = #{id}")
end
end
如果您通过erubis -x
运行一点ERB(注意:Rails使用Erubis而不是普通ERB)来查看将其转换为Ruby的方式,您将看到类似以下的内容:
_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 << '
';
第二行在这里很有趣。 Erubis已将您的评论直接添加到生成的代码中,而无需关心或理解其效果。 那应该告诉您为什么您看到该特定错误消息。
这里有一些教训:
如果删除HTML注释内的Ruby注释:
<!-- <h4><%= employee.first_name %></h4></br> -->
然后事情应该重新开始。 或者更好的方法是删除整个HTML注释,因为它只是这样。
尝试将表单标签包装在“ ()
”中,URL参数应如下所示:
<%= form_for(:pr_add_ons_deductions, :url => {:controller => "your-controller-name", :action => "your-action-name"}) do |f| %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.