轨道3阿贾克斯形式不发送PARAMS [英] Rails 3 Ajax Form Not Sending Params

查看:131
本文介绍了轨道3阿贾克斯形式不发送PARAMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我使用排序记录我的索引页形式,我想添加一些Ajax功能,但由于某种原因,它不是送我的表单参数,只要我添加了Ajax的组合。

I have a form on my index page that I'm using to sort records, I wanted to add some Ajax functionality to it but for some reason it's not sending my form parameters as soon as I added Ajax to the mix.

下面是我的方式:

<div id="sort-options">
    <%= form_tag uploads_path, :method => "get", :remote => true do %>          
            <%= select_tag "sort", options_for_select([["Creation Date", "created_at"], ["File Name", "name"], ["Rating", "rating_average"], ["Downloads", "downloads"]]) %>
            <%= select_tag "direction", options_for_select([["Descending", "desc"], ["Ascending", "asc"]]) %>                   
            <%= submit_tag "Sort" %>
    <% end %>   
</div>

和我阿贾克斯的javascript:

And my Ajax javascript:

jQuery(document).ready(function($) 
{ 
    $(function () {  
        $('#sort-options input, .pagination a').live("click", function () {  
            $.get(this.href, null, null, 'script');  
            return false;  
        });  
    });

});

当我点击提交按钮,看看我的Firebug的控制台来看看发生了什么很明显,它成功地调用Aj​​ax和它所服务的页面没有问题。然而,它完全忽略了我的PARAMS被发送,并没有考虑到这一点,当它重新查询数据库。如果我参加了':远程=>真正的',并删除链接到阿贾克斯文件的PARAMS成功地送回到index操作 - 只是没有阿贾克斯:(

When I click the submit button and look at my Firebug console to see what's happening it's clear that it's succesfully calling the Ajax and it serves the page no problem. However it completely ignored my params that are sent and it doesn't take that into account when it re-queries the database. If I take out the ':remote => true' and remove the link to the Ajax file the params succesfully get sent back to the index action - just no Ajax :(

我要补充一点,阿贾克斯目前任职分页,我有非常相同的页面上。

I should add that the Ajax currently works for pagination that I have on the very same page.

推荐答案

现在的问题是,绑定的另一个单击处理程序到你的提交按钮,它覆盖了Rails的内置的AJAX功能(:远程=&GT;真正的)。你的选择#排序选项输入,.pagination一个匹配不仅分页链接,而且表单的提交按钮。按钮没有HREF属性,这就是你传递给 $。获得()为URL。因此, $。获得(this.href,...)试图使一个AJAX GET请求空URL(其作用就好像它是一个空字符串,从而当前的URL,没有任何GET参数)。

The problem is that you bind another click handler to your submit button which overrides Rails's built-in AJAX functionality (:remote => true). Your selector #sort-options input, .pagination a matches not only pagination links, but also the submit button of the form. Buttons do not have href attributes, which is what you pass to $.get() as a URL. So $.get(this.href, ...) tries to make an AJAX GET request to a null URL (which acts as if it was an empty string, thus the current URL, without any GET params).

这篇关于轨道3阿贾克斯形式不发送PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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