简体   繁体   English

Ruby on Rails和Ajax

[英]Ruby on Rails + Ajax

I am trying to update a div in my rails application. 我正在尝试在Rails应用程序中更新div。 Iam just learning ROR. 我只是在学习ROR。 So Its a learning stage for me.Please find the code. 所以对我来说这是一个学习阶段。请找到代码。 In view page... 在查看页面中...

<%= javascript_tag do %>
     jQuery(function($) 
{

     $("#tabs4").click(function()
     {      
        $.ajax(
        {
        url:'/spaces/showcal',
        type:'GET'

        });
      });

});


<% end %>

In spaces controller.. 在空间控制器..

def showcal

    respond_to do |format|

     format.html 
     {
      render (:update) { |page|
      page.replace_html 'tab4', :partial => 'spaces/showcal'
    }}
    end
end

What am I doing wrong here.. Please help I also have a partials page (_showcal)which has some text to display into that div. 我在这里做错了..请帮助我也有一个局部页面(_showcal),其中有一些文本要显示到该div中。

You should request /spaces/showcal.js and react to format.js . 您应要求/spaces/showcal.js和反应format.js

I never worked with $.ajax , you might need to set additional parameters there. 我从未使用过$.ajax ,您可能需要在那里设置其他参数。 With script.aculo.us, something like this works: 使用script.aculo.us,可以执行以下操作:

new Ajax.Request('/tasks/#{task.id}.js',
    {asynchronous:true, evalScripts:true, method:'get'});

When using ajax, a good way to debug this kind of thing is see what response the server returns. 使用ajax时,调试这种情况的一种好方法是查看服务器返回的响应。 If it looks good, then you know your javascript code needs to change to get it working, if it looks wrong, then fix the server side code first. 如果看起来不错,那么您就知道需要更改JavaScript代码才能使其正常工作,如果看起来不对,请首先修复服务器端代码。

Here's how i would do it: 这是我的方法:

def showcal
  render :layout => false
end


#showcal.html.erb
<%= render :partial => "spaces/showcal" %>

Your js block: 您的js区块:

jQuery(function($) 
{
  $("#tabs4").click(function()
    {      
      $.ajax({
        url:'/spaces/showcal',
        type:'GET',
        success: function(data)
        {
          $('#tab4').html(data);
        }
      });
    });
});

I generally prefer using javascript as supposed to rails built in code when it comes to replacing and using content with ajax. 当涉及到用ajax替换和使用内容时,我通常更喜欢使用javascript来构建内置代码。 I have to admit, because of this, I'm not sure if your update action was mostly correct or not. 因此,我不得不承认,我不确定您的更新操作是否大部分正确。 The page.replace_html should work if you're using the prototype helpers. 如果您正在使用原型帮助器,则page.replace_html应该可以使用。 The only thing is because you are doing an ajax request to achieve it and the resulting body would contain that code, and it would not execute on your current dom. 唯一的原因是因为您正在执行ajax请求以实现该请求,并且生成的主体将包含该代码,并且不会在您当前的dom上执行。 So i think that was probably executing, but because it's on a separate page response, it didnt do anything. 所以我认为这可能正在执行,但是因为它在单独的页面响应中,所以它什么也没做。

Give my suggestions a try, and let me know how it goes. 尝试一下我的建议,让我知道如何进行。 Sorry that my answer is a bit hazy. 对不起,我的回答有点朦胧。

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

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