最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mix ruby code with js.coffee script - Stack Overflow

programmeradmin0浏览0评论

I have ajax links on my view, on which I want to check password before they're sent to the actual action, as I am using devise controller, i am restricted to the use of specific password check. Following is the coffee script I wish to use for the validation.

.<%= link_to "CANCEL PAYMENT", { :action => "some_action", :info => n.id },class: "css_class",:remote => true %>

I am using the above link

I am looking for a following kind of code.

$("a.css_class").live "click", ->
      password_variable = prompt("Enter password", "password")
      if |ruby-code|current_user.valid_password?(password_variable)|ruby-code|
        true
      else
        alert "You entered wrong password"
        false

How'll the ruby code work with coffee script mix.

I have ajax links on my view, on which I want to check password before they're sent to the actual action, as I am using devise controller, i am restricted to the use of specific password check. Following is the coffee script I wish to use for the validation.

.<%= link_to "CANCEL PAYMENT", { :action => "some_action", :info => n.id },class: "css_class",:remote => true %>

I am using the above link

I am looking for a following kind of code.

$("a.css_class").live "click", ->
      password_variable = prompt("Enter password", "password")
      if |ruby-code|current_user.valid_password?(password_variable)|ruby-code|
        true
      else
        alert "You entered wrong password"
        false

How'll the ruby code work with coffee script mix.

Share Improve this question edited Nov 30, 2012 at 17:04 Nishutosh Sharma asked Nov 30, 2012 at 16:58 Nishutosh SharmaNishutosh Sharma 1,9362 gold badges26 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

If it's a code for you static asset then it's obvious you won't be able to put some server-side dynamics into it. It'll be converted into a plain JavaScript-snippet and placed into your app's public folder.

If you have your view called *.coffee then you already have done all the preparations. The views named this way will be automatically pre-processed with the ERb engine (via <%= ... %>):

in views/some/thing.coffee:

alert "Server's time is <%= Time.now %>"

I had the same doubt.

Background:

I am writing an ERP for my pany. It uses messages in Spanish, English and Japanese.

I am using coffeescript, haml, scss, NO ERB

So, multilingual messages work fine in all my views, but, I added a .js library for replacing the browser's ugly dropbox with a nice bobox with dropdown list and it uses a hash for holding the messages in local languajes.

So what I did was:

_form.html.haml

:coffeescript
  menssages_for_select2 [
    "#{I18n.t('select.formatNoMatches')}"
    "#{I18n.t('select.formatInputTooShort')}"
    "#{I18n.t('select.formatInputTooLong')}"
    "#{I18n.t('select.formatSelectionTooBig')}"
    "#{I18n.t('select.formatLoadMore')}"
    "#{I18n.t('select.formatSearching')}"
  ]

I do this in the view, so I can have access to I18n library. If I try to access the I18n library inside a .js.coffee, it fails

now, in

mycode.js.coffee

@mensajes_select2 = (txt) ->
  $.extend $.fn.select2.defaults,
    formatNoMatches: ->
      txt[0]
    formatInputTooShort: (input, min) ->
      txt[1]
    formatInputTooLong: (input, max) ->
      txt[2]
    formatSelectionTooBig: (limit) ->
      txt[3]
    formatLoadMore: (pageNumber) ->
      txt[4]
    formatSearching: ->
      txt[6]
发布评论

评论列表(0)

  1. 暂无评论