新手问题 View 当中的 CoffeeScript 的问题

skpark1987 · 2017年04月27日 · 最后由 kai209209 回复于 2017年05月02日 · 1339 次阅读

当前场景是,当我点击画面中的编辑链接时会调用edit.js.erb执行相关处理, 当我把该处理文件重命名为edit.js.coffee时(也就是使用 coffeescript),相同的内容无法执行,也不会输出任何错误 log

// 环境   ruby: 2.4.0
//        rails: 5.0.2
// index,用的模板是slim
= link_to note.title, edit_note_path(note), remote: true

// controller
def edit
    @note = Note.find(params[:id])
    respond_to do |format|
       format.html
       format.js {render layout: false}
    end
end

// edit.js.erb
note = <%= @note.to_json.to_s.html_safe %>;
title = note['title']
action_path = "<%= note_path(@note) %>";
form_template = `
<form accept-charset="UTF-8" action="${action_path}" method="PUT">
  <input name="_method" type="hidden" value="put" />
  <input name="utf8" type="hidden" value="&#x2713;" />
  <input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>" />
  <div class="field">
    <label for="note_title">Title</label>
    <input type="text" name="note[title]" id="note_title" value="${note['title']}"/>
  </div>
  <div class="field">
    <label for="note_content">Content</label>
    <textarea name="note[content]" id="note_content">${note['content']}</textarea>
  </div>
  <div class="field">
    <input type="submit" name="commit" value="Create" class="btn btn-primary" data-disable-with="Create">
  </div>
</form>
`
$("#note_form").html(form_template);

能帮我看看,代码有什么问题吗。

不要在 Views 里面写 CoffeScript 无法 Cache,会导致每次都转换 CoffeScript -> JavaScript

因为你里面带了变量,理论上来说是无法 Cache 的

不能 cache 和不能运行有什么关联吗,还是我使用了不能使用的语法呢。 恩,views 里还是使用原生 js 了。

曾经遇到过类似的,你的 layout application.html.erb 格式是不是被你改成了 application.slim 了?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号