找不到原因:语法错误,ROR 应用中出现意外'>'



好吧,这可能很愚蠢,但我花了一整晚的时间试图在我的代码中找到此错误的原因,但没有运气。

我的最后一个选择是看看你的眼睛是否比我的看得更好。

我在下面的行(第 68 行(中收到此错误:

<%= image_tag image_product.image.url(:medium), class: "img-responsive", id: "<%= index == 0 ? 'zoom_05' : '' %>" %>

完整片段如下:

<div class='carousel-inner '>
<% @product.images.each_with_index do |image_product, index| %>
<div class=<%= index == 0 ? 'item active' : '' %> >
<%= image_tag image_product.image.url(:medium), class: "img-responsive", id: "<%= index == 0 ? 'zoom_05' : '' %>" %>
</div>
<% end %>
<script>
$("#zoom_05").elevateZoom({ zoomType    : "inner", cursor: "crosshair" });
</script>
</div>
<!-- sag sol -->
<a class='left carousel-control' href='#carousel-custom' data-slide='prev'>
<span class='glyphicon glyphicon-chevron-left'></span>
</a>
<a class='right carousel-control' href='#carousel-custom' data-slide='next'>
<span class='glyphicon glyphicon-chevron-right'></span>
</a>
</div>

和完整的错误消息:

syntax error, unexpected '>'
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:74: unknown regexp options - crpt
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:75: syntax error, unexpected '<'
</div>
^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:78: unknown regexp options - pa
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:79: syntax error, unexpected '<'
</a>
^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:81: unknown regexp options - pa
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:82: syntax error, unexpected '<'
</a>
^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:83: unknown regexp options - dv
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:84: syntax error, unexpected '<'
<!-- thumb -->
^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:84: syntax error, unexpected unary-, expecting keyword_do or '{' or '('
<!-- thumb -->
^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:86: syntax error, unexpected keyword_do_LAMBDA
...oduct.images.each_with_index do |image_product, index| 
...                               ^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:86: syntax error, unexpected '|', expecting '='
...index do |image_product, index| 
...                               ^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:90: syntax error, unexpected keyword_end, expecting ')'
'.freeze;             end 
^
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:135: syntax error, unexpected keyword_ensure, expecting ')'
/Users/dadi/Documents/Vefir/stores/hlinreykdal/app/views/products/show.html.erb:137: syntax error, unexpected keyword_end, expecting ')'

你不能嵌套<% %>,你需要使用字符串插值,像这样:

<%= image_tag image_product.image.url(:medium), class: "img-responsive", id: "#{index == 0 ? 'zoom_05' : ''}" %>

虽然,由于你已经在 ruby 代码中,你根本不需要任何类型的插值,所以你可以这样做:

<%= image_tag image_product.image.url(:medium), class: "img-responsive", id: index == 0 ? "zoom_05" : "" %>

快速猜测是这样的:

<%= image_tag image_product.image.url(:medium), class: "img-responsive", id: "<%= index == 0 ? 'zoom_05' : '' %>" %>

应该是这样的:

<%= image_tag image_product.image.url(:medium), class: "img-responsive", id: "#{index == 0 ? 'zoom_05' : ''}" %>

具体来说,交换:

<%= index == 0 ? 'zoom_05' : '' %>

为:

#{index == 0 ? 'zoom_05' : ''}

您的问题很可能您已经在<%= %>行中,因此您无法在其中再次执行<%= %>

您滥用<%=来不必要地执行字符串插值。

<%=标签是 ERB 的一部分,您无法嵌套它们。在<% %>中,您不再处于ERB上下文中,而是在编写普通的Ruby。如果要执行字符串插值,则应使用"#{ ... }"。但是,这里根本不需要字符串插值。

您的行应为:

<%= image_tag image_product.image.url(:medium), class: "img-responsive", id: (index == 0 ? 'zoom_05' : '') %>

最新更新