【问题标题】:How to use Rails helpers in coffee script?如何在咖啡脚本中使用 Rails 助手?
【发布时间】:2015-01-02 10:05:37
【问题描述】:

在我的视图中,我会打开一个新的用户页面,如下所示:

<% @users.each do |user| %>
  <tr>
    <td><%= link_to 'Show', user %></td>
    <td><%= link_to 'Edit', edit_user_path(user) %></td>
    <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>

如何在咖啡脚本中做类似的事情?我有以下情况。用户上传了几个文件,上传完所有文件后,他应该被重定向到他的个人文件存储库。这是咖啡脚本:

jQuery ->
  $('#new_update').fileupload
    dataType: "script"
    add: (e, data) ->
      types = /(\.|\/)(gif|jpe?g|png)$/i
      file = data.files[0]
      if types.test(file.type) || types.test(file.name)
        data.context = $(tmpl("template-upload", file))
        $('#new_update').append(data.context)
      else
        alert("#{file.name} is not a gif, jpeg, or png image file")

      $("#submit_button").on 'click', ->
        data.submit()

    progress: (e, data) ->
      if data.context
        progress = parseInt(data.loaded / data.total * 100, 10)
        data.context.find('.bar').css('width', progress + '%')

    stop: (e, data) ->
      /* I need to redirect him here */

所有文件上传后stop 被触发,我需要将用户重定向到他的个人页面。我怎样才能做到这一点?我知道我可以像这样将他重定向到主页:

window.location "/"

问题是如何将@user 从视图中获取到咖啡脚本。谢谢。

【问题讨论】:

    标签: jquery ruby-on-rails ruby-on-rails-4 coffeescript jquery-file-upload


    【解决方案1】:

    首先添加一个数据属性

    #new_update{:"date-target" => @user.id}

    然后做

    window.location.href= "/users/" + $("#new_update").attr("data-target")

    【讨论】:

    • 谢谢!它看起来不错,但由于某种原因,我在 window.location 上收到一个错误,表明它不是一个函数。有什么问题?
    • window.location.href = "/users/" + $("#new_update").attr("data-target")
    猜你喜欢
    • 2012-07-10
    • 2011-11-08
    • 1970-01-01
    • 2019-05-02
    • 2018-05-26
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多