使用带有计数器的js附加Rails标记

时间:2014-03-26 05:31:14

标签: javascript jquery ruby-on-rails ruby-on-rails-4

尝试为不同的报告创建合并工具,并希望允许合并多个报告。卡在这里:

<div class="row">
                <div class="col-md-6">
                    <%= form_tag(merge_reports_path, method: :put) do %>
                    <div class="form-group">
                        <%= select_tag(:report_1, options_from_collection_for_select(current_user.reports,"id", "name"), class: "form-control" ) %>  

                    </div>
                    <a class="add-one">Add 1+</a>
                    <div class="form-group">
                        <%= select_tag(:to_merge_report, options_from_collection_for_select(current_user.reports,"id", "name"), class: "form-control" ) %>
                    </div>



                    <% end %>
                </div>
            </div>
            <script>
                var counter = 1
                $(".add-one").click(function(){

                    counter ++;
                    $(this).prepend('<div class="form-group"> <%= j select_tag(:report_' + counter + ' , options_from_collection_for_select(current_user.reports,"id", "name"), class: "form-control" ) %> </div> ')

                })
            </script>

出现此错误:

/Users/mmahalwy/Desktop/Code/dossiyadata/app/views/reports/index.html.erb:35: syntax error, unexpected tSTRING_BEG, expecting ')' ...append=( j select_tag(:report_' + counter + ' , options_from... ... ^ /Users/mmahalwy/Desktop/Code/dossiyadata/app/views/reports/index.html.erb:35: syntax error, unexpected ',', expecting ')' ...t_tag(:report_' + counter + ' , options_from_collection_for_... ... ^ /Users/mmahalwy/Desktop/Code/dossiyadata/app/views/reports/index.html.erb:35: syntax error, unexpected ',', expecting ')' ...ent_user.reports,"id", "name"), class: "form-control" ) );@o... ... ^ /Users/mmahalwy/Desktop/Code/dossiyadata/app/views/reports/index.html.erb:35: syntax error, unexpected ')', expecting keyword_end ...me"), class: "form-control" ) );@output_buffer.safe_append='... ... ^ /Users/mmahalwy/Desktop/Code/dossiyadata/app/views/reports/index.html.erb:65: syntax error, unexpected keyword_do_block, expecting keyword_end '; @reports.each do |report| ^ /Users/mmahalwy/Desktop/Code/dossiyadata/app/views/reports/index.html.erb:97: syntax error, unexpected keyword_ensure, expecting end-of-input

1 个答案:

答案 0 :(得分:1)

代码出错,您无法像在代码中一样使用符号添加数字。你需要使用字符串

"report_#{counter}"

这是最终代码

$(this).prepend('<div class="form-group"> <%= j select_tag("report_#{counter}" ,   options_from_collection_for_select(current_user.reports,"id", "name"), class: "form-control" ) %> </div> ')