将 ruby 代码与 js.coffee 脚本混合



我的视图上有ajax链接,我想在将它们发送到实际操作之前检查密码,因为我使用的是设计控制器,所以我只能使用特定的密码检查。以下是我希望用于验证的咖啡脚本。

.<%= link_to "取消付款", { :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

Ruby 代码将如何与咖啡脚本混合一起使用。

如果它是静态资产的代码,那么很明显您将无法在其中放入一些服务器端动态。它将被转换为普通的 JavaScript 代码段,并放入应用的 public 文件夹中。

如果你的观点叫做*.coffee,那么你已经完成了所有的准备工作。以这种方式命名的视图将使用 ERb 引擎自动预处理(通过 <%= ... %>):

views/some/thing.coffee中:

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

我也有同样的疑问。

背景:

我正在为我的公司编写一个ERP。它使用西班牙语,英语和日语的消息。

我正在使用咖啡脚本,haml,scss,没有ERB

因此,多语言消息在我的所有视图中都可以正常工作,但是,我添加了一个.js库,用于将浏览器丑陋的保管箱替换为带有下拉列表的漂亮组合框,并且它使用哈希来保存本地语言中的消息。

所以我所做的是:

_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]

最新更新