语法错误,意外的“<”,期待“)”
syntax error, unexpected '<', expecting ')'
我正在尝试在 Rails 上的 Ruby 中构建一个表单。问题是,当我启动 table 本身时,它一直给我一个语法错误,意外的 '<',期待 ')'。
这是表格上方的几行:
<% @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
尝试用“()
”包装您的表单标签,URL 参数应如下所示:
<%= form_for(:pr_add_ons_deductions, :url => {:controller => "your-controller-name", :action => "your-action-name"}) do |f| %>
如果您通过 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 已将您的评论直接放入生成的代码中,而不关心或理解其影响。这应该会告诉您为什么会看到该特定错误消息。
这里有一些教训:
- Ruby ERB 中的注释不像 Ruby 代码中的注释那样工作。 ERB 处理器并不真正理解 Ruby,它们只是散布一些文本以尝试生成有效的 Ruby 代码。
- ERB 也不理解 HTML 评论,HTML 评论只是更多的文字。
- 不要注释掉代码来禁用它,而是使用修订控制并删除代码。
如果删除HTML评论内的Ruby评论:
<!-- <h4><%= employee.first_name %></h4></br> -->
然后事情应该会重新开始工作。或者更好的是,删除整个 HTML 评论,因为它只是在路上。
我正在尝试在 Rails 上的 Ruby 中构建一个表单。问题是,当我启动 table 本身时,它一直给我一个语法错误,意外的 '<',期待 ')'。
这是表格上方的几行:
<% @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
尝试用“()
”包装您的表单标签,URL 参数应如下所示:
<%= form_for(:pr_add_ons_deductions, :url => {:controller => "your-controller-name", :action => "your-action-name"}) do |f| %>
如果您通过 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 已将您的评论直接放入生成的代码中,而不关心或理解其影响。这应该会告诉您为什么会看到该特定错误消息。
这里有一些教训:
- Ruby ERB 中的注释不像 Ruby 代码中的注释那样工作。 ERB 处理器并不真正理解 Ruby,它们只是散布一些文本以尝试生成有效的 Ruby 代码。
- ERB 也不理解 HTML 评论,HTML 评论只是更多的文字。
- 不要注释掉代码来禁用它,而是使用修订控制并删除代码。
如果删除HTML评论内的Ruby评论:
<!-- <h4><%= employee.first_name %></h4></br> -->
然后事情应该会重新开始工作。或者更好的是,删除整个 HTML 评论,因为它只是在路上。