【问题标题】:Unable to load Rails code in Javascript string无法在 Javascript 字符串中加载 Rails 代码
【发布时间】:2017-02-27 15:16:16
【问题描述】:

这是我的部分代码:

    $("table#table_field_type_users > tbody").append(`
<tr id='<%= @field_type_user.id%>' class='fields_type_user'>
  <td><input type='text' name='name_field_type_users' value='<%= j @field_type_user.name %>' class='form-control' id='name_field_type_user'/></td>
  <td><button type='button' name='delete_field_type_users' class='btn btn-danger' id='delete_field_type_users'>-</button></td>
</tr>
`);

但是当 JQuery 将以下内容附加到页面时,它会以纯文本形式呈现:

<tr id="<%= @field_type_user.id%>" class="fields_type_user">
  <td><input name="name_field_type_users" value="<%= j @field_type_user.name %>" class="form-control" id="name_field_type_user" type="text"></td>
  <td><button type="button" name="delete_field_type_users" class="btn btn-danger" id="delete_field_type_users">-</button></td>
</tr>

编辑:

代码的第一部分是一个名为 create.js 的文件的一部分,它在方法创建成功时呈现...似乎在升级到 Rails 5 之前它工作得很好...

有过程:

  1. 用户按下页面上的按钮,对 Create 方法进行 Ajax 调用。
  2. create 方法完成工作并渲染:

    格式.js

【问题讨论】:

  • 这是在.js.erb?
  • 不,这是一个 .js 文件 编辑:谢谢你,它工作得很好!我是不是很傻!
  • 那么你不能执行rails代码:)
  • 是的!谢谢 ! :)

标签: javascript jquery ruby-on-rails


【解决方案1】:

试试这个:

$("table#table_field_type_users > tbody").append("
    <tr id='<%= @field_type_user.id%>' class='fields_type_user'>
        <td><input type='text' name='name_field_type_users' value='<%=@field_type_user.name %>' class='form-control' id='name_field_type_user'/></td>
        <td><button type='button' name='delete_field_type_users' class='btn btn-danger' id='delete_field_type_users'>-</button></td>
    </tr>");

【讨论】:

    猜你喜欢
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多