0

我是 rails 的新手。我想通过从下拉列表中选择值来使用 jquery 对内容进行排序。所以我在使用 jquery 将表单提交到特定的控制器操作时遇到问题。我有一个包含许多产品的类别模型。在我的类别显示中,我有一个如下形式:

    <%= form_for(:category, :url => sort_category_path, method: 'GET', :class=> " form-inline pull-left") do |f| %>
              Sort By :
              <select id ="sort" name= "order_by">
                <option>Default</option>
                <option>Name</option>
                <option>Pirce</option>

              </select>
  <%end%>

在这里,我不确定 :category 是什么意思?用于提交表单的 Jquery:

  $('select#sort').change(
function(e){

    $(this).closest('form').trigger('submit');

});

然后在我的类别控制器中,我有一个这样的 sort_item 操作:

  def sort_item
   @category_sort = Category.find(params[:id])

   @products = @category_sort.products.order("#{params[:order_by].to_s} ASC")
  end

我不知道如何发送对请求的响应。服务器日志说:

在 2013-11-11 12:53:49 +0600 开始 GET "/en/categories/tshart/sort_item?utf8=%E2%9C%93&order_by=Pirce" for 127.0.0.1 12:53:49 +0600 由 CategoriesController#sort_item 作为 HTML 参数处理:{ "utf8"=>"✓", "order_by"=>"Pirce", "locale"=>"en", "id"=>"tshart"} 类别加载 (0.3ms) SELECT categories.* FROM categoriesWHERE categories. slug= 'tshart' 限制 1

重定向到http://test.com/en/categories/tshart 过滤器链因 :find_category 渲染或重定向而停止 已完成 301 在 6.7 毫秒内永久移动(ActiveRecord:0.3 毫秒)

请记住 test.com 代表 localhost:3000 。

i want to know what i am doing wrong here ,why the sorting is not working or is there any better way to accomplish my sorting task?

please help me . thanks in advance.

4

2 回答 2

0

Try putting your change event in doc ready handler:

$(function(){ // <----enclosed in document ready
    $('select#sort').change(function(e){
       $(this).closest('form').trigger('submit');
    });
});

This will look for if dom has been ready for js event execution bind on elements.

于 2013-11-11T07:28:07.747 回答
0

Wow its working now. It was a routing issue I explained earlier.The issue was:

Redirected to http://test.com/en/categories/tshart Filter chain halted as :find_category rendered or redirected Completed 301 Moved Permanently in 6.7ms (ActiveRecord: 0.3ms)

Here my request was redirected to another action then i removed the the filter chain and then added render :show to my categories controller sort_item action. now its just rocking .Everything else remain the same.

Hope this question may help others.

Thanks @jai for your help.

于 2013-11-11T10:00:58.643 回答