简体   繁体   English

包含JavaScript的Rails JSON响应

[英]Rails json response that includes javascript

I have an application that responds to .json (json format). 我有一个响应.json(json格式)的应用程序。 I also have to build a bookmarklet that makes a div on the page where you execute it. 我还必须构建一个书签,在执行它的页面上创建一个div。 This bookmarklet makes a request to the application and the application should respond with a json object that contains the javascript. 此小书签向应用程序发出请求,并且应用程序应使用包含javascript的json对象进行响应。 Let's say this is all the javascript is: 假设这就是所有的javascript:

function selectElementContents()
{
  el = document.getElementById('quicklink_value')
  if (document.body.createTextRange) {
        // IE
        var textRange = document.body.createTextRange();
        textRange.moveToElementText(el);
        textRange.select();
        textRange.execCommand("Copy");
    }
  else if (window.getSelection && document.createRange) {
        // non-IE
        var range = document.createRange();
        range.selectNodeContents(el);
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
        var range = document.createRange();
        try {
        var successful = document.execCommand('copy');
        var msg = successful ? 'successful' : 'unsuccessful';
    } catch(err) {
        console.log('Oops, unable to copy');
      }
    }
}
content = "<%= escape_javascript(render(:partial => 'events/bookmarklet').html_safe) %>";
_tp_bookmarklet(content);

As you can see I also have a partial that needs to be rendered inside a variable named content. 如您所见,我还有一部分需要在名为content的变量中呈现。

def bookmarklet
    respond_to do |format|
      format.json
    end
  end

This is the action that is being called by the bookmarklet. 这是小书签正在调用的操作。 How can I put the javascript(with the content variable that contains the rendered partial in it) as the json response? 如何将javascript(具有包含呈现的部分内容的content变量)作为json响应?
I can provide any additional info that is needed. 我可以提供所需的任何其他信息。

You can do it with simply 您可以简单地做到这一点

@content = escape_javascript(render(:partial => 'events/bookmarklet').html_safe) 

and then 接着

format.json {content: @content}

and in you front end you could you can get the json response and response will have your html. 并且在您的前端,您可以获取json响应,并且响应将包含您的html。

another way is to to do is to directly render the partial/template and sen it off as a json object... but that will work when you have a way to cater the controller variables in the partial... like if you are using controller variables either instance or local you should take care of those. 另一种方法是直接渲染partial / template并将其作为json对象检测出来……但是当您有一种方法可以满足partial中的控制器变量时,它将起作用……就像您正在使用控制器变量无论是实例变量还是局部变量,您都应该注意。

format.json { content: escape_javascript(render(:partial => 'events/bookmarklet').html_safe) }

or what you can do is a create a method may be private to render out your partial like 或者您可以做的是创建一个methodmethod可能是私有的,以呈现出您的部分

def blahblah (params...)
    escape_javascript(render(:partial => 'events/bookmarklet').html_safe)
end
........
format.json {content: blahblah(params)}

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

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