我有一个功能不同的rails项目,在这个项目中,我试图更新一些表单控件,以便在AJAX上工作,而不是HTML。但是我的控制器继续将它处理为HTML而不是JS,我不知道为什么。
我试图在用户/index.html.erb中设置远程链接:
<%= link_to 'Disable', disable_user_path(user), :confirm => 'Are you sure?',
:method => :put, :remote => true %>
呈现为:
<a href="/users/1/disable" data-confirm="Are you sure?" data-method="put"
data-remote="true" rel="nofollow">Disable</a>
我的用户控制器:
def disable
@user = User.find(params[:id])
if @user.update_attribute(:enabled, false)
respond_to do |format|
format.html { redirect_to root_path, :flash => { :success => "Disabled." }}
format.js {}
end
else
redirect_to root_path, :flash => { :error => @user.errors.full_messages }
end
end
我在application.html.erb中包括:
<%= javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "//ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", "jquery.rails.js", "application" %>
我想我从一个标准的地方删除了第三和第四个文件,但如果这些可能是罪魁祸首,我可以查看他们的内部或跟踪更多的信息,他们。
当我测试它时,它不会提示我“你确定吗?”它用重定向和“禁用”闪存消息进行响应,服务器日志确认它被作为html处理。
为127.0.0.1启动GET“/users/1/禁用”,2012-02-11 17:17:31 -0200由UsersController#disable作为HTML进行处理
发布于 2012-02-11 22:40:26
如果您还没有这样做,您可能仍然需要包含不显眼的javascript适配器。
发布于 2012-02-12 04:52:21
看起来jQuery没有被加载。
检查包含标记,特别是资源路径.
<%= javascript_include_tag "//ajax.googleapis.com/ajax/libs/....." %>
大多数浏览器将//...
解释为本地资源,就像浏览器的本地资源一样。
将URL粘贴到浏览器中,并查看它是否会加载。我打赌它会回到file://
计划,然后抱怨(在你的本地机器上)没有被找到。
要么在那里添加一个http://
方案,要么去掉双斜杠。
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/....." %>
# or...
<%= javascript_include_tag "ajax.googleapis.com/ajax/libs/....." %>
发布于 2012-02-13 00:10:04
问题的最终是,我的包括没有抓取所有的文件,显然是必要的。我按自述文件中的指令从零开始安装了我的javascripts目录,并将include更改为:
<%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js", :defaults %>
https://stackoverflow.com/questions/9243498
复制相似问题