如何通过HAML模板访问Javascript中的ruby实例变量 考虑使用嵌套表格...或类似的变体形式 ...不能那样做 ...但是有解决方案:A渲染部分并根据需要隐藏B异步获取部分

问题描述

我有一个正在用CommentController渲染的局部图像。让我们称其为_edit_comment.html.haml部分,我在其中传递实例变量@comment。

$("#edit-comment-#{object.id}").html('#{ escape_javascript(render :partial => 'edit_comment',locals: { story: @comment.story,comment: @comment }) }')

在我的部分中,我有一个JS,当单击一个按钮时,我想调用它。

= form_with model: [story.industry,story,comment],class: "w-100" do |f|
  %fieldset.w-100                                    
    = f.text_area :content,rows: 2,hide_label: true,class: 'px-2 py-1',placeholder: "Leave a comment" 
  %fieldset  
    = f.submit "Update",class: 'btn submit text-white mt-2'   
    %span.btn.cancel.text-white.mt-2 Cancel-
:javascript
  $(document).ready( function() {
    $('.cancel').click(e => {
      console.log("#{@comment}")       
    })
  } );

我知道“#{}”适用于字符串,但是我希望能够访问整个对象。目前,控制台的结果为

#<Comment:0x000055971a9c5278>

我需要获取整个ruby对象,因为我想将其作为变量传递给将要呈现的另一部分。像这样:

:javascript
  $(document).ready( function() {
    $('.cancel').click(e => {
      $("#edit-comment-#{object.id}").html('#{ escape_javascript(render :partial => 'edit_comment',locals: { story: "#{@comment}".story,comment: "#{@comment}" }) }')
    })
  } );

这可能吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)