Thursday 15 September 2011

jquery - update query for instance variable -



jquery - update query for instance variable -

with help have got params beingness passed method in controller, stuck on how utilize method , assign instance variable.

class publiccontroller < applicationcontroller def rehome @animals = animal.animals_rehome.paginate(:page => params[:page], :per_page => 6) respond_to |format| format.html format.js end end def rehomed respond_to |format| format.js {render :json => animal.where(animal_type: params[:animal_type]) } end end

the params updated next ajax post

$('select.btn').on('change', function() { var animal_type = $('#animaltype option:selected').text(); var animal_town = $('#animaltown option:selected').text(); $.ajax({ type: 'post', url: '/public/rehomed', data: { animal_type: animal_type, }, success: function(data) { console.log(data); } }); });

what want accomplish on initial page load use

def rehome @animals = animal.animals_rehome.paginate(:page => params[:page], :per_page => 6) end

but when selecting filters via select dropdown menu want

def rehome @animals = rehomed.paginate(:page => params[:page], :per_page => 6) end

but im not sure how set out, if point me in direction of resources work out or illustration can larn off grateful

thanks

edit

my setup looks like

class publiccontroller < applicationcontroller before_filter :default_animal def rehome respond_to |format| format.html format.js end end def rehomed conditions = {} conditions.merge!(animal_type: params[:animal_type]) if params[:animal_type].present? conditions.merge!(rehomed: params[:rehomed]) if params[:rehomed].present? conditions.merge!(users: {town: params[:animal_town]}) if params[:animal_town].present? @animals = animal.joins(:user).where(conditions) respond_to |format| format.js { render json: @animals } end end private def default_animal @animals = animal.animals_rehome.paginate(:page => params[:page], :per_page => 6) end end

my ajax request sends right parameters build the @animals query each time dropdowns updated in view , accomplish results of new query loaded onto page in align will_paginate construction if thats @ possible please

$('select.btn').on('change', function() { var animal_type = $('#animaltype option:selected').text(); var animal_town = $('#animaltown option:selected').text(); var data_send = { animal_type: animal_type, animal_town: animal_town, rehomed: false} if(animal_type == 'all') { data_send = {animal_town: animal_town, rehomed: false} } if(animal_town == 'all') { data_send = {animal_type: animal_type, rehomed: false} } $.ajax({ type: 'post', url: '/public/rehomed', data: data_send, success: function(data) { console.log(data); } }); });

view rehome.html.erb

<div class="all_animals"> <%= render @animals %> # in partial called _animal.html.erb </div> <div id="infinite-scrolling"> <%= will_paginate @animals %> </div>

rehome.js.erb

$('.all_animals').append('<%= j render @animals %>'); <% if @animals.next_page %> $('.pagination').replacewith('<%= j will_paginate @animals %>'); <% else %> $(window).off('scroll'); $('.pagination').remove();

you should utilize before_filter

class publiccontroller < applicationcontroller before_filter :filter_method def rehome respond_to |format| format.html format.js end end def rehomed respond_to |format| format.js {render :json => @animals.where(animal_type: params[:animal_type]) } end private def filter_method @animals = animal.animals_rehome.paginate(:page => params[:page], :per_page => 6) end end

you need params[:page] , params[:animal_type] rehomed action

more on before_filter

jquery ruby ajax json ruby-on-rails-4

No comments:

Post a Comment