通过一些帮助我现在已经将params传递给我的控制器中的方法,我现在坚持的是如何使用该方法并将其分配给我的实例变量。
class PublicController < ApplicationController
def rehome
@animals = Animal.animals_rehome.paginate(:page => params[:page], :per_page => 6)
respond_to do |format|
format.html
format.js
end
end
def rehomed
respond_to do |format|
format.js {render :json => Animal.where(animal_type: params[:animal_type]) }
end
end
使用以下ajax帖子更新参数
$('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);
}
});
});
我想要实现的是在初始页面加载时使用
def rehome
@animals = Animal.animals_rehome.paginate(:page => params[:page], :per_page => 6)
end
但是当我通过我想要的选择下拉菜单选择我的过滤器时
def rehome
@animals = rehomed.paginate(:page => params[:page], :per_page => 6)
end
但是我不知道怎么设置这个,如果有人能指出我在某些资源的方向来解决这个问题或者一个例子,那么我可以学习它我将非常感激
由于
修改
我的设置现在看起来像
class PublicController < ApplicationController
before_filter :default_animal
def rehome
respond_to do |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 do |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
我的ajax请求发送正确的参数以在每次在视图中更新下拉列表时构建@animals查询,我想要实现的是将新查询的结果加载到页面上以与之对齐will_paginate结构,如果可能的话请
$('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);
}
});
});
查看rehome.html.erb
<div class="all_animals">
<%= render @animals %> # this is in a 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();
答案 0 :(得分:0)
你应该使用before_filter
class PublicController < ApplicationController
before_filter :filter_method
def rehome
respond_to do |format|
format.html
format.js
end
end
def rehomed
respond_to do |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
你需要params [:page]和params [:animal_type]来重新加入行动
更多关于before_filter