如何将内联红宝石代码与我的客户端RJS混合使用?

时间:2015-11-17 16:14:02

标签: ruby-on-rails faye rjs private-pub

我正在使用PrivatePub,而我在这里遇到了一些奇怪的行为。在详细介绍之前,请注意我使用的是Ruby SELECT { } ON COLUMNS, { ([Inventory information].[BOM].[BOM].ALLMEMBERS * [Inventory information].[Item number].[Item number].ALLMEMBERS * [Inventory information].[Line number].[Line number].ALLMEMBERS * [Inventory information].[Parent Item].[Parent Item].ALLMEMBERS * [Inventory information].[Product name].[Product name].ALLMEMBERS * [Inventory information].[Quantity].[Quantity].ALLMEMBERS * [Inventory information].[Sequence].[Sequence].ALLMEMBERS )} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM [Inventory analysis] CELL PROPERTIES VALUE 和Rails SELECT { } ON COLUMNS, { ([Inventory information].[BOM].[BOM].ALLMEMBERS * [Inventory information].[Item number].[Item number].ALLMEMBERS * [Inventory information].[Line number].[Line number].ALLMEMBERS * StrToMember ('[Inventory information].[Parent Item].[Parent Item].&[' + @InventoryinformationParentItem + ']') * [Inventory information].[Product name].[Product name].ALLMEMBERS * [Inventory information].[Quantity].[Quantity].ALLMEMBERS * [Inventory information].[Sequence].[Sequence].ALLMEMBERS )} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM [Inventory analysis] CELL PROPERTIES VALUE

我们说1.9.3

3.2.2

TestsController

class TestsController < ApplicationController
  def index
  end

  def create
    respond_to do |format|
      format.js
    end
  end
end

tests/index.html.erb

<h1>Test</h1>

<%= content_for :footer do %>
  <%= subscribe_to tests_path  %>
<% end %>

简单直接。现在我的问题是每当我运行tests/create.js.erb动作时,由于某种原因<% publish_to tests_path do %> alert("Working fine"); if (false) {alert("This never run");}; if (false) {<% puts "*"*100 %>}; <% end %> 总是运行(即使我做了if(false)语句)。

我知道我可以使用ruby create然后应该正常工作,例如:

<% puts "*"*100 %>

但是我仍然想做一些客户端验证(例如检查div是否存在然后相应地渲染一些部分),如:

if

但似乎 <% if false %> <% puts "*"*100 %> <% end %> 似乎会一直呈现。

如何处理此问题并使用客户端if语句和ruby模板代码?目前看来if ($("#chat").length){ $("#chat").append("<%= j render(@messages) %>"); } 内的任何内容都会被触发,我不想要这种行为。

1 个答案:

答案 0 :(得分:1)

始终打印

puts "*"*100,因为在处理js.erb文件期间,文件中的所有Ruby代码都会被执行。

JavaScript代码仅在浏览器中运行时执行,因此{ER}处理文件时if (false)语句无效。

所以,这与PrivatePub gem无关,但这是因为你混合了JS和Ruby代码。

对于你的上一个(聊天)代码示例,请尝试render_to_string,但这可能还不够,因为在编译JS文件期间@messages变量为空。

我想补充一点,混合JS和Ruby会导致一些令人头疼的问题。有时最好在视图文件中使用Ruby / ERB在HTML元素上设置数据属性,然后让JS代码读取并使用这些数据属性的值。这样您就不必在JS代码中使用Ruby代码了。

相关问题