简体   繁体   中英

How to use Rails helpers in coffee script?

Inside my view I would open a new user page simple like this:

<% @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 %>

How can I do something similar in coffee script? I have the following situation. User uploads a couple of files and after all files are uploaded he should be redirected to his personal repository of files. This is the coffee script:

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 */

After all files are uploaded stop gets triggered and there I need to redirect user to his personal page. How can I do that? I know I can redirect him to home page like this:

window.location "/"

The problem is how to get @user from view to coffeescript. Thank you.

First add a data attribute

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

Then do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM