简体   繁体   English

语法错误,意外的“ <”,应为“)”

[英]syntax error, unexpected '<', expecting ')'

I'm trying to build a form in Ruby on Rails. 我正在尝试在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: 除此之外,还有一个控制器中私有的params方法:

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

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: 如果您通过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 << '
';

The second line is of interest here. 第二行在这里很有趣。 Erubis has thrown your comment right into the generated code without caring or understanding its effect. Erubis已将您的评论直接添加到生成的代码中,而无需关心或理解其效果。 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中的Ruby注释不能像Ruby代码中的注释那样工作。 ERB processors don't really understand Ruby, they just sling bits of text around in an attempt to generate valid Ruby code. ERB处理器并不真正了解Ruby,它们只是在周围散列一些文本,以尝试生成有效的Ruby代码。
  2. ERB doesn't understand HTML comments either, HTML comments are just more text to sling around. ERB也不理解HTML注释,HTML注释只是更多文本。
  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: 如果删除HTML注释内的Ruby注释:

<!-- <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. 或者更好的方法是删除整个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM