簡體   English   中英

Rails資產和javascript集成

[英]Rails assets and javascript integration

前提:我對javaScript缺乏了解。

在rails布局中,以下腳本起作用:

<script>
  $(function() {
    $( "#date_available_dal" ).datepicker({dateFormat: 'yy-mm-dd', showButtonPanel: true});
    $( "#date_available_al" ).datepicker({dateFormat: 'yy-mm-dd', showButtonPanel: true});
  });
</script>

但是,如果我在資產/ javascripts文件內的.js文件中剝離了打開和關閉腳本標簽的同時放置了相同的代碼,則瀏覽器將加載該文件,但是[正如Curly會說的]沒有任何反應。

我已經經歷過很多次了,這阻礙了我正確使用管道。 Rails在后台執行的操作可能會更改此腳本的行為?

按照相同的思路,如果我要附加給定應用程序方法的語言環境

  def set_locale
    if user_signed_in?
      I18n.locale = current_user.idioma.code.downcase
    else
      I18n.locale = params[:locale] || I18n.default_locale
    end
  end

datepicker({dateFormat: 'yy-mm-dd', regional[ #{set_locale} ]

在堆棧之間如何處理此代碼?

更新資料

當前處於開發模式,瀏覽器正在接收帶有以下內容的文件:

  $(function() {
$( "#date_available_dal" ).datepicker({dateFormat: 'yy-mm-dd', minDate: 0, numberOfMonths: 3, showButtonPanel: true});
$( "#date_available_al" ).datepicker({dateFormat: 'yy-mm-dd', minDate: 1, numberOfMonths: 3, showButtonPanel: true});
  });

回答主要問題:約瑟夫(Josef)已確定問題所在。 在這種情況下,要解決JavaScript的問題。

我將添加第二個觀察結果:require_tree。 它的功能(按字母順序查看文件),聲明的內容等等之間可能非常棘手。 異構使用JSes的應用程序越大,越應該越用它來避開形式,通常聲明個別情況下真正需要什么。

至於第二個問題,鑒於這些頁面已編入索引,我將再次進行適當的練習並發布答案。

jQuery的datepicker具有自己的I18n文件集,其定義為datepicker-de.js 從github下載必要的文件,然后將其保存在需要的位置。 我已經使用Idoma模型在idioma_id字段中定義了用戶的語言,以便說明

<% if user_signed_in? %>
  <%= javascript_include_tag ("datepicker-" + "#{current_user.idioma.code.downcase}") %>
<% elsif params[:locale].present? %>
  <%= javascript_include_tag ("datepicker-" + "#{params[:locale]}") %>
<% else %>
<% end %>

最后的else語句是一個快捷方式,如果您的默認語言環境為en,則該快捷方式適用。 datepicker的默認值為en,因此已經加載。

暫無
暫無

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

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