0

I am new to rails.i Want to sort content using jquery by selecting value from dropdown list. so i am having problem to submit a form to a specific controller action using jquery. I have a category model which has many products.In my category show i have a form like bellow:

    <%= 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%>

here at this form i am not sure what does mean by :category ? Jquery for submit the form :

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

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

});

Then at my category controller i have a sort_item action like this:

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

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

i dont know how to send response to the request. the server log says :

Started GET "/en/categories/tshart/sort_item?utf8=%E2%9C%93&order_by=Pirce" for 127.0.0.1 at 2013-11-11 12:53:49 +0600 Processing by CategoriesController#sort_item as HTML Parameters: {"utf8"=>"✓", "order_by"=>"Pirce", "locale"=>"en", "id"=>"tshart"} Category Load (0.3ms) SELECT categories.* FROM categories WHERE categories.slug = 'tshart' LIMIT 1

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)

keep in mind that test.com stands for 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.

2 Answers 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.

1
  • my bad.its already is in document ready.its hitting to the desired url @jai
    – monsur
    Nov 11, 2013 at 7:31
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.