gpt4 book ai didi

jquery - 语法错误: unexpected POST_IF error Rails

转载 作者:行者123 更新时间:2023-12-03 23:04:36 27 4
gpt4 key购买 nike

我在 app/assets/javascripts/ticket.js.coffee 中为特定 View 运行 jQuery。每次我访问该页面时,浏览器都会呈现此错误 - 在线任何地方都没有提及此错误。

(localhost:3000/Tickets/new):

SyntaxError: unexpected POST_IF

Extracted source (around line #6):

3: <head>
4: <title>Ops2</title>
5: <%= stylesheet_link_tag "application", :media => "all" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>

引发错误的页面文件 -

app/views/tickets/_form.html.erb:

<%= form_for(@ticket) do |f| %>
<% if @ticket.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@ticket.errors.count, "error") %> prohibited this ticket from being saved:</h2>
<ul>
<% @ticket.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :school_id %><br />
<%= f.collection_select :school_id, School.order(:name), :id, :name, include_blank: true%>
</div>

<div class="field">
<%= f.label :location_id %><br />
<%= f.grouped_collection_select :location_id, School.order(:name), :locations, :name, :id, :name, include_blank: true%>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>

app/assets/javascripts/application.js 包含:

//= require jquery
//= require jquery_ujs
//= require_tree .

带有 jQ​​uery 的 Coffeescript 文件 -

app/assets/javascripts/tickets.js.coffee:

$ ->
$('#ticket_location_id').parent().hide()
locations = $('#ticket_location_id').html()
$('#ticket_school_id').change ->
school = $('#ticket_school_id :selected').text()
options = $(locations).filter("optgroup[label='#{school}']").html()
if options
$('#ticket_location_id').html(options)
$('#ticket_location_id').parent().show()
else
$('#ticket_location_id').empty
$('#ticket_location_id').parent().hide()

POST_IF 错误已通过在我的 CoffeeScript 中缩进 if/else 语句得到解决! (有关详细信息,请参阅下面的答案)没有错误并且页面加载!

最佳答案

您的 if 语句缺少缩进。缩进在 CoffeeScript 中至关重要

这个...

if options
$('#ticket_location_id').html(options)
$('#ticket_location_id').parent().show()
else
$('#ticket_location_id').empty
$('#ticket_location_id').parent().hide()

...需要是这样,前导缩进不是可选的:

if options
$('#ticket_location_id').html(options)
$('#ticket_location_id').parent().show()
else
$('#ticket_location_id').empty
$('#ticket_location_id').parent().hide()

关于jquery - 语法错误: unexpected POST_IF error Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15826116/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com