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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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
Rails-尝试创建简单的ajax示例_Ajax_Ruby On Rails 3 - Fatal编程技术网

Rails-尝试创建简单的ajax示例

Rails-尝试创建简单的ajax示例,ajax,ruby-on-rails-3,Ajax,Ruby On Rails 3,由于某些原因,下面的代码不起作用。当我点击“test”时,它会将我重定向到/home/test,并且不显示任何内容 view/home/index.html.erb <%= link_to "test", { :controller => "home", :action => "test" }, :remote => true %> <div id='test_div'> </div> $("#test_div").html("som

由于某些原因,下面的代码不起作用。当我点击“test”时,它会将我重定向到/home/test,并且不显示任何内容

view/home/index.html.erb

<%= link_to "test", { :controller => "home", :action => "test" }, :remote => true %>
   <div id='test_div'>
</div>
$("#test_div").html("some text");
控制器

class HomeController < ApplicationController
 def test
    "test123 test456"
    respond_to() do |format|
      format.js {render :layout => false}
    end
  end
end
class HomeControllerfalse}
结束
结束
结束

我应该如何使用ajax刷新div_测试?

路由文件是什么样子的?你能把它贴在这里吗?执行rake路由并发布输出

以下是一些可能对您有所帮助的事情:

  • 另外,请查看和链接到远程的部分。
    link\u to\u remote
    功能不在Rails中。您使用的是什么版本的rails?Bash:
    $rails-v
  • 看看S.O.上关于AJAX on Rails调用的内容
  • 编辑:以下是关于这个问题的答案,可能会有所帮助

  • 编辑:再看一遍,我想你不需要在
    回复
    链接
    中的
    {…}
    。看看第三个链接。我认为你需要把页面放在一个部分,并呈现它。它现在没有呈现,因为它的
    render:layout=>false

    使用控制台查看您到远程的链接是否有效,并使用console.log获取响应,我认为您需要将$更改为jQuery('#test_div').html(“一些测试”);那里没有什么有用的东西。我数到两个以上,但是+2表示更多!好。。。我几乎迟到了5年,但我修正了“2”的问题哈哈!