Rails的阿贾克斯:.js.erb呈现为文本,而不是JS [英] Rails Ajax: .js.erb rendered as text, not JS

查看:114
本文介绍了Rails的阿贾克斯:.js.erb呈现为文本,而不是JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用rails3渲染JavaScript的一个麻烦(如果这是相关的,我使用jQuery和它,因为它是在应用程序的许多其他部分用于正常工作),而不是HTML。

I'm having a trouble with rails3 to render Javascript (If this is relevant, I use JQuery and it works fine since it's used in numerous other parts of the application) instead of html.

我有: 我一看,我有此链接:

What I have: I a view, I have this link:

<%= link_to 'Edit', edit_user_repreneur_path(@user_repreneur, :format => :js), :remote => true %>

首先,我真的不明白为什么我要补充:格式=>:JS去使用JS视图控制器,而不是HTML之一。 下面是控制器的code:

First of all, I don't really get why I have to add :format => :js to get the controller to use the JS view, and not the HTML one. Here is the code of the controller:

def edit
    @user_repreneur = UserRepreneur.find_by_id_view(params[:id])
    respond_to do |format|
      format.js
      format.html
    end 
end

在控制器类的开始,我有这样一行:

At the beginning of the controller class, I have this line:

  respond_to :js, :html

我的edit.js.erb文件包含一个警告一行。 然而,呈现页面时,对JS呈现为文本,并且因此不执行。 我试图改变format.js行

My edit.js.erb file contains a single line with an alert. However, when rendering the page, the JS is rendered as text, and therefore is not executed. I tried to change the format.js line to

format.js { render :layout => false }

但它不会改变任何东西。 我想我失去了一些东西,但我实在看不出有什么...

But it doesn't change anything. I guess I am missing something, but I really don't see what...

感谢很多提前,干杯!

PS:我不知道这是有帮助的,但在此之前我添加了:格式=>:在我的链接帮手JS位,有人总是呈现为HTML,如果我评论的的format.html线,我在服务器端有一个406无法接受的错误。

PS: I don't know if this is helpful, but before I add the :format => :js bit in my link helper, the view was always rendered as HTML, and if I commented the format.html line, I had a 406 Not Acceptable error on the server side.

推荐答案

我不认为这个问题是与你的服务器端code。这一切对我来说很好。而事实上,它是渲染 edit.js.erb 证明了你的服务器端code是工作的罚款。

I don't think the problem is with your server-side code. It all looks good to me. And the fact that it is rendering edit.js.erb proves that your server-side code is working fine.

现在的问题是关于您的客户端。当它接收Ajax响应,所以不执行它作为的JavaScript。它正在执行它作为文本。

The problem is on your client-side. When it receives the Ajax response, it isn't executing it as javascript. It is executing it as text.

最近我有这个问题我自己,我想我有我的code与我,但我在工作。不过,我个人写的,而不是依靠 jQuery的Ajax请求:远程=&GT;真正的,因为多用于形式的选择。

I have recently had this problem myself and I wish I had my code with me but I'm at work. However, I personally wrote the jQuery Ajax request instead of relying on :remote => true since that option is mostly used in forms.

试着给你的的link_to 的ID,然后把这个jQuery的在你的的application.js 或其它地方。

Try giving your link_to an id and then put this jQuery in your application.js or wherever.

$('#link_id').click(function(){
   $.ajax({
      type : 'GET',
      url : '/:controller/:action/',
      dataType : 'script'
   });
   return false;
});

这会抢了链接的点击事件,将请求发送到它应该返回你的 edit.js.erb 文件,然后执行它作为脚本的服务器。

This will grab the link's click event, send a request to the server which should return your edit.js.erb file, and then execute it as script.

你要知道,有很多的安全问题考虑,以及真实性令牌等。不过,这应该让您的警报来执行,让你在正确的道路来完成您的应用程序上。

Mind you, there is a lot of security concerns to take into account as well as authenticity tokens and the like. However, this should get your alert to execute and get you on the right path to completing your app.

阅读上 jQuery的阿贾克斯进一步的选项和详细信息发送的数据也是如此。

Read up on jQuery's Ajax for further options and details about POSTing data as well.

希望这有助于。

更新:其他可能的解决方案包括使用UJS作为一个Javascript的驱动程序。您可以阅读 https://github.com/rails/jquery-ujs 中的文档和查看Railscast的话题非侵入式JavaScript 使用UJS。

UPDATE: Other possible solutions include using UJS as a Javascript driver. You can read the documentation at https://github.com/rails/jquery-ujs and view a Railscast on the topic Unobtrusive Javascript using UJS.

这篇关于Rails的阿贾克斯:.js.erb呈现为文本,而不是JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆