Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails js.erb响应未向Haml部分发送局部变量_Ruby On Rails_Ajax - Fatal编程技术网

Ruby on rails Rails js.erb响应未向Haml部分发送局部变量

Ruby on rails Rails js.erb响应未向Haml部分发送局部变量,ruby-on-rails,ajax,Ruby On Rails,Ajax,我意识到这与其他一些问题类似,但我已经经历了相当多的问题,我认为我正在按照他们的建议去做,但我的代码仍然不起作用 因此,正如标题所说,我试图从js.erb文件加载一个部分(响应AJAX调用)。但是,它不能正常工作 full_list.html.haml: =link_to(test_path(@work), id: "privacy-button", remote: true) do = render "privacy_button_content", locals: {:show_ot

我意识到这与其他一些问题类似,但我已经经历了相当多的问题,我认为我正在按照他们的建议去做,但我的代码仍然不起作用

因此,正如标题所说,我试图从js.erb文件加载一个部分(响应AJAX调用)。但是,它不能正常工作

full_list.html.haml:

=link_to(test_path(@work), id: "privacy-button", remote: true) do
    = render "privacy_button_content", locals: {:show_others => @work.show_others}
-if locals[:show_others] == false
    -test_string = "twas false"
-else
    -test_string = "twas true"
%p = test_string
_隐私按钮内容.html.haml:

=link_to(test_path(@work), id: "privacy-button", remote: true) do
    = render "privacy_button_content", locals: {:show_others => @work.show_others}
-if locals[:show_others] == false
    -test_string = "twas false"
-else
    -test_string = "twas true"
%p = test_string
works_controller.rb:

 def test_function 
   @work.model_function
   respond_with(:layout => false)
 end
test_function.js.erb:

$("#privacy-button").html("<%= escape_javascript render(:partial => 'privacy_button_content', locals: {:show_others => @work.show_others}) %>");
$(“#隐私按钮”).html('privacy#u button_content',locals:{:show_others=>@work.show_others})%>”;
因此,完整的#u列表中有privacy#u按钮,该按钮呈现部分,单击#privacy按钮的响应应该是修改控制器中的某些内容,并获得response.js.erb

这在页面加载时可以正常工作(因此将本地消息传递给partial确实有效),但是每当我运行AJAX调用时,我都会收到

(undefined local variable or method `locals' for #<#<Class:0x00000005006338>:0x000000068481f8>):
(未定义的局部变量或#的“locals”方法):

任何帮助都将不胜感激。

更新
\u privacy\u button\u content.html.haml
如下:

-if show_others == false  
    -test_string = "twas false"
-else
    -test_string = "twas true"
%p = test_string
在本地哈希中传递
show_others
时,将为您创建一个名为
show_others
的本地变量。您可以在部分中以
show_others
的形式访问它,而不是
locals[:show_others]


请参阅Rails指南中的

因此,我已经找到了答案。似乎我在使用haml时遇到的特定错误与渲染语法有关(我在视图中指定
render:partial
时遇到了相同的问题,因此它不是js.erb独有的)

partial.js.erb中的代码现在如下所示:

$("#privacy-button").html("<%= escape_javascript render("privacy_button_content", :locals => {:show_others => @work.show_others}) %>");

只要您适当地更新您的局部变量(参考
var
而不是
locals[:var]
),这将起作用。

应用Kirti Thorat的更改,然后更改

= render "privacy_button_content", locals: {:show_others => @work.show_others}
为了

还要记住,视图不应该有逻辑,它应该在帮助程序装饰程序


顺便说一句:我认为
%p=test\u string
应该是
%p=test\u string
嘿,科蒂,谢谢你回复我。我只是尝试了一下,但不幸的是,它不起作用(在它在初始页面加载时加载分部,但不是从javascript加载之前,通过此更改,它不会在页面加载时加载。)我知道我可以将它们作为“show_others”=>@work.show_others传递,这将使您为分部推荐的语法起作用,但我改用了本地人散列,因为我看到一些人说这可能会对我的问题有所帮助(尽管没有)。我认为我的问题可能与在哈姆尔和雇员再培训局之间移动有关,所以我现在正在努力解决这个问题。谢谢你回复我。正如我对Kirti所提到的,我知道这种语法是可能的,以及如何从两端使用它,但是通过这种方式传递它与使用本地散列并不会对我的问题产生影响(实际上我是首先这样做的)。但是,我将编辑我的回复,以提及这种语法也是可能的,并且可能更高级。