-1

I have some javascript that displays some data from my database on my webpage. I also have a button that when the user clicks it some logic occurs in the controllers and then I want to have the data displayed changed. However, at the momment when they click the button nothing happens (page is not reloaded). Could you guys help me set that up? Here is what I have so far:

controller:

class QtlsController < ApplicationController
 require 'json'
 require 'uri'
 $rollback = nil
 def index
   logger.debug "\nrollback is: #{$rollback}\n"
   render "index"
 end
 def rollback
   $rollback = params[:version].gsub(/"/,'')
   redirect_to :action => :index
 end
end

view:

  <%- if $rollback.nil? %>
     generates one type of table
  <%- else %>
     generates another type of table
  <%- end %>
  ...some other logic and buttons etc...
  jQuery(function() {
            jQuery( "#rollback").button();
            jQuery( "#rollback").click(function() {
                    var version = getSelectedText('version_selector');
                    jQuery.ajax({
                            type: "POST",
                            headers: {
                                    'X-Transaction': 'POST Example',
                                    'X-CSRF-Token': jQuery('meta[name="csrftoken"]').attr('content')
                            },
                            url: "/qtls/rollback",
                            data: {version: JSON.stringify(version) },
                            dataType: 'json',
                            sucess: function() { alert("Success! Rollbacked"); }
                    });
            });

Here is what is in log files when I go to the page and when I click the rollback button:

 Started GET "/qtls" for 10.64.229.59 at Tue Jul 17 16:48:52 -0500 2012
 Processing by QtlsController#index as HTML
 rollback is: 
 ROLLBACK IS NIL!!!
   Qtl Load (2.0ms)  SELECT `qtls`.* FROM `qtls` 
   Rendered qtls/index.html.erb within layouts/application (404.8ms)
   Rendered shared/_user_nav.html.erb (1.3ms)
   Rendered shared/_nav.html.erb (1.3ms)
   Rendered shared/_footer.html.erb (0.6ms)
 Completed 200 OK in 544ms (Views: 540.9ms | ActiveRecord: 2.0ms)
 Started POST "/qtls/rollback" for 10.64.229.59 at Tue Jul 17 16:48:57 -0500 2012
 Processing by QtlsController#rollback as JSON
   Parameters: {"version"=>"\"test1\""}
 Redirected to http://10.10.136.244:4000/qtls
 Completed 302 Found in 3ms (ActiveRecord: 0.0ms)
 Started GET "/qtls" for 10.64.229.59 at Tue Jul 17 16:48:58 -0500 2012
 Processing by QtlsController#index as JSON
 rollback is: test1
   Rendered qtls/index.html.erb within layouts/application (37.7ms)
   Rendered shared/_user_nav.html.erb (3.3ms)
   Rendered shared/_nav.html.erb (4.3ms)
   Rendered shared/_footer.html.erb (2.0ms)
 Completed 200 OK in 99ms (Views: 97.4ms | ActiveRecord: 0.0ms)
4

1 回答 1

1

$rollback使用像!这样的全局变量是一种非常糟糕的风格。

如果您想存储用户数据,请使用您的会话对象。

使用 jquery-rails gem 在您的视图中与 jQuery 集成,然后:remote => :true在您的表单中使用,以便 rails 可以处理表单提交。

于 2012-07-18T21:01:25.513 回答