【问题标题】:Rails: pass data to javascript [duplicate]Rails:将数据传递给javascript [重复]
【发布时间】:2013-08-12 07:13:33
【问题描述】:

我是 Rails 的新手,我有一个控制器:

class PagesController < ApplicationController
   def home
      @temp = "Hello"
   end
end

我已经读到我必须将 javascript 代码放在 application.js 中(告诉我是否属实)并且我有:

window.onload=function(){alert("<%= j @temp %>")}

显然,警报打印字符串“” 如何将变量 @temp 传递给 javascript,以便警报可以打印 Hello?

谢谢

【问题讨论】:

标签: javascript ruby-on-rails


【解决方案1】:

我在how to pass Ruby objects to the client 上写了一篇文章。 Ryan Bates 也有出色的 RailsCast on passing data to JS

向您的视图添加一个与您的 PagesControlle#home 操作相对应的 div,该操作在您加载页面时将不可见,但将包含存储在 Ruby 对象中的数据:

# views/pages_controllers/home.html.erb
<%= content_tag :div, class: "temp_information", data: {temp: @temp} do %>
<% end %>

加载包含此 div 的页面并查看页面源代码。您可以看到存储在.temp_information div 中的Ruby 对象。打开 JavaScript 控制台以将 Ruby 对象作为 JavaScript 对象访问:

$('.temp_information').data('temp')

您不需要将您的 JS 添加到 JS 部分,您也可以使用资产管道。

【讨论】:

  • 您的回答对我有用。谢谢!
【解决方案2】:

我做了类似的事情,但比 gon 更简单。我的ApplicationController 中有以下内容。

def javascript_variables(variables)
  @javascript_variables ||= {}
  @javascript_variables.merge!(variables)
end

在控制器操作中,我可以执行类似的操作

def some_action
  javascript_variables(user: current_user)
end

在我的ApplicationHelper 我有这样的东西

def javascript_variables(variables = nil)
  @javascript_variables ||= {}
  @javascript_variables.merge!(variables) and return if !variables.nil?

  output  = ''
  padding = @javascript_variables.keys.group_by(&:size).max.first

  @javascript_variables.each do |variable, value|
    output << "#{variable.to_s.ljust(padding)} = #{value.to_json},\n          "
  end

  raw "var " + output.strip.html_safe.gsub(/\,\Z/m, ';')
end

最后在我的布局中 &lt;head&gt; 我有

<script>
  <%= javascript_variables %>
</script>

这给了我类似的东西(来自我应用程序中的一个真实示例)

<script>
  var pageModule        = "site/index",
      isCustomer        = false,
      utype             = "normal",
      isAnonymous       = true,
      keyboardShortcuts = false,
      pubnub            = null,
      requestToken      = "3zj974w074ftria3j";
</script>

【讨论】:

  • 这种方法会不会让网站更容易受到攻击
  • 我不确定这是一个问题还是一个陈述,所以我不知道如何回答。
  • 你不应该创建这么多的全局变量。而是创建一个对象并将值存储为其属性。
  • 您的方法很棒,但是如果没有从控制器传递的 javascript 变量,您的助手代码会导致错误。正确的代码是@javascript_variables ||= {} \n @javascript_variables.merge!(variables) if !variables.nil? \n return if @javascript_variables == {} 我用\n 只是为了拆分注释中的代码行。
【解决方案3】:

看看这个。

http://tech.thereq.com/post/17243732577/rails-3-using-link-to-remote-true-with-jquery-ujs

最简单的方法之一是使用 js.erb 文件,您可以在其中使用 ruby​​ 标签来访问您在控制器操作中定义的变量。

您需要在控制器动作中使用 respond_to 块,指定动作能够响应 javascript。

items_controller.rb

class ItemsController < ApplicationController

  def action
    respond_to do |format|
      format.js
      #format.html {}    #  These are for allowing other types of formats to be responded to.
      #format.json {}    #  But they are not necessary for using this js.erb way of doing things.
    end
  end

end

/views/items/action.js.erb

$(div).html('The cat has erased your page');

【讨论】:

    猜你喜欢
    • 2013-02-08
    • 2016-04-12
    • 2015-10-05
    • 2012-11-13
    • 2021-05-24
    • 2019-06-14
    • 2012-03-10
    • 1970-01-01
    • 2016-04-08
    相关资源
    最近更新 更多