简体   繁体   English

使用JavaScript更改ruby循环变量

[英]Changing a ruby loop variable with JavaScript

Within my controller I have two ways of sorting my database of images: 在我的控制器中,我有两种方式对图像数据库进行排序:

def index
   @picslist = Pic.order("created_at DESC")

   @picslist2 = Pic.order("price DESC")
end

Within my view I lay them out: 在我看来,我将它们布置为:

<% @picslist.each do |pic| %>
    PICS IN HERE
<% end %>

I want to alter the @picslist variable to @picslist2 with a JavaScript click event, something like so: 我想使用JavaScript click事件将@picslist变量更改为@picslist2 ,如下所示:

$("#sortbyprice").click(function(event) {

    //replace @picslist with @picslist2

});

Is that the right way of doing it? 这是正确的做法吗? Either way I'd be grateful if you could recommend a graceful way of doing this! 无论哪种方式,如果您能推荐一种优美的方式,我将不胜感激!

Many thanks in advance. 提前谢谢了。

making two objects @picslist and @picslist2 is not good idea. 制作两个对象@picslist和@ picslist2不是一个好主意。 Pass the sort_by params when javascript event occured. 发生javascript事件时传递sort_by参数。

def index
   @picslist = Pic.order("#{params[:sort_by]} DESC")  
end

where params[:sort_by] will be "created_at" or "price" 其中params [:sort_by]将为“ created_at”或“ price”

Something like that and use ajax or form submit to update the page. 诸如此类,并使用ajax或表单提交来更新页面。

感谢您的帮助...实际上,我发现最好的方法实际上是对结果进行排序,如此处所述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM