开发者

Passing a variable through AJAX and Rails

I've made an array in my controller with the items that I would like to rotate through. The initial set is used as a partial collection and looped through.

render :partial => "partial", :collection => @array[1..4]

I also have a link_to_remote link on the page that makes a POST request to the controller to change the range on the collection.

link_to_remote "Next", :url => {:act开发者_如何学Cion => "update"}, :update => "partial_div"

I would like to have an incremental counter that is returned and then subsequently posted again to move forward. Like this:

respond_to do |format|
   format.js{
      @counter += 4
      render :partial => "partial", :collection => @array[@counter..(@counter + 4)]
   }

Thanks!


You could make a javascript function in your view that in the first time it is called (on the page loading) sets a global variable to 1. When called again, it sets this variable to the number from the argument.

In your controller, instead of rendering the partial, call an .erb.js file that updates the content of the partial_div and calls this function setting the variable to the new value.

In your link_to_remote add this variable as a parameter to the controller and remove the :update => "partial_div"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜