簡體   English   中英

使用js.coffee腳本混合ruby代碼

[英]Mix ruby code with js.coffee script

我的視圖上有ajax鏈接,我想在發送到實際操作之前檢查密碼,因為我使用的是設計控制器,我僅限於使用特定的密碼檢查。 以下是我希望用於驗證的咖啡腳本。

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

我正在使用上面的鏈接

我正在尋找以下類型的代碼。

$("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

紅寶石代碼如何與咖啡腳本混合使用。

如果它是您靜態資產的代碼,那么顯然您將無法將一些服務器端動態放入其中。 它會被轉換成一個純JavaScript的片段,並放置到您的應用程序的public文件夾。

如果您的視圖名為* .coffee,那么您已經完成了所有准備工作。 以這種方式命名的視圖將使用ERb引擎自動預處理(通過<%= ... %> ):

views /some/thing.coffee中:

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

我有同樣的疑問。

背景:

我正在為我的公司編寫ERP。 它使用西班牙語,英語和日語的消息。

我正在使用coffeescript,haml,scss,NO ERB

因此,多語言消息在我的所有視圖中都能正常工作,但是,我添加了一個.js庫,用一個帶有下拉列表的漂亮的組合框替換瀏覽器的丑陋的dropbox,它使用哈希來保存本地語言中的消息。

所以我做的是:

_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')}"
  ]

我在視圖中執行此操作,因此我可以訪問I18n庫。 如果我嘗試訪問.js.coffee中的I18n庫,則會失敗

現在,在

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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM