简体   繁体   English

将数据从控制器传递到Rails中的资产js

[英]Pass data from controller to assets js in rails

I am using rails 5.I try to pass the data from controller (which is taken from sql query) to js file in assests. 我正在使用Rails 5.我试图将控制器中的数据(从sql查询中获取)传递给assss中的js文件。 And I run the query from controller as 我从控制器运行查询为

@sample = Sample.all  

I need to display data in fullcalendar events. 我需要在全日历事件中显示数据。
assets/js file 资产/ js文件

$('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'agendaDay,listWeek,agendaWeek,month'
            },
            editable: true,
            eventLimit: true, // allow "more" link when too many events
            defaultView: 'listWeek',
            navLinks: true,
            events: [
                <% @sample.each do |sample| %>
                {
                    title: '<%= sample.title %>',
                    start:  TODAY + 'T09:00:00',
                    description: '<%= sample.description %>'
                }
                <% end %>  

it didn't show the result in fullcalendar. 它没有以全日历显示结果。 How to pass it from controller to asset/js 如何将其从控制器传递到Asset / js
Thanks much for quick response 非常感谢您的快速回复

A simple method is to place a partial in layouts/_variables.html.erb , then render it in a <script> tag in your application layout. 一种简单的方法是将局部放置在layouts / _variables.html.erb中 ,然后将其呈现在应用程序布局的<script>标记中。

For example: 例如:

<script>    
  _app.samples = [
  <% @sample.each do |sample| %>
       {
         title: '<%= sample.title %>',
         start:  TODAY + 'T09:00:00',
         description: '<%= sample.description %>'
      }
  <% end %>  
  ];
</script>

Then inside your original calendar, just use the generated Javascript directly: 然后在原始日历中,直接使用生成的Javascript:

  $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'agendaDay,listWeek,agendaWeek,month'
        },
        editable: true,
        eventLimit: true, // allow "more" link when too many events
        defaultView: 'listWeek',
        navLinks: true,
        events: _app.samples,

In layouts/application.html.erb layouts / application.html.erb中

</script>
  <%= render partial: "layouts/_variables" %>
  <%= csrf_meta_tags %>
  ...

It has the benefit that you do not need to pollute you Javascript assets with Rails code, and data is updated on every page refresh without extra round-trips to the server. 它的好处是您不需要使用Rails代码污染Javascript资产,并且每次刷新页面时都会更新数据,而无需额外往返服务器。

The disadvantage is that you need to consider Ajax requests separately (although that is a whole different question), and should take care to place these variables in an app specific structure so as not to pollute your global space with random variables. 缺点是您需要单独考虑Ajax请求(尽管这是一个完全不同的问题),并且应注意将这些变量放置在应用程序特定的结构中,以免用随机变量污染全局空间。

You can use gon gem for this problem, like the below example: 您可以使用gon gem解决此问题,例如以下示例:

in controller file: 在控制器文件中:

gon.user_id = @user.id gon.user_id = @ user.id

in js file: 在js文件中:

console.log(gon.user_id); console.log(gon.user_id);

" Gon gem — get your Rails variables in your js. If you need to send some data to your js files and you don't want to do this with long way through views and parsing - use this force! " Gon gem-在您的js中获取Rails变量。如果您需要将一些数据发送到js文件中,并且您不想长时间通过视图和解析来执行此操作,请使用此力!

For more info: 有关更多信息:

https://github.com/gazay/gon https://github.com/gazay/gon

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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