简体   繁体   中英

Passing a param from javascript to controller

I have the following javascript in a View:

<script type="text/javascript">
    var pusher = new Pusher('<%= Pusher.key %>'); // Replace with your app key
    var channel = pusher.subscribe('choices');

    channel.bind('created', function(data) {
        var after = $('.ticket:last').attr('data-time');
        $("#tickets").append("<%=raw escape_javascript(render(@tickets, :after => " + after + ")) %>");
    });
</script>

And the following in my Controller:

@tickets  = Choice.where("choices.created_at > ?", Time.at(params[:after].to_i + 1).utc)

I'm looking for a way to pass the after param from the javascript to the controller, så it only returns the Choices where the created_at are higher then the last Choice displayed right now.

I guees this line ain't doing the job, so a bit of help with this would very much appriciated :)

$("#tickets").append("<%=raw escape_javascript(render(@tickets, :after => " + after + ")) %>");

EDIT:

I now have the following, which seems to fix the passing of the after variable to the controller.. I checked Firebug and can see the response conteains the data on the newest BET.. But the view is still wrong. Instead of appending the newest Bet to the DIV it shows all the past BETS twice without the new one at all. Anyone? :)

View file:

<script type="text/javascript">
    var pusher = new Pusher('<%= Pusher.key %>'); // Replace with your app key
    var channel = pusher.subscribe('choices');

    channel.bind('created', function(data) {
        var after = $('.ticket:last').attr('data-time');

        var settings = { after: after}
        $.ajax({
            url: '/Home/Index',
            type: 'POST',
            dataType: 'json',
            data: settings,
            cache: true,
            success: function (data) {
                $("#tickets").append("<%=raw escape_javascript(render(@tickets)) %>");
                }
          });
        }
    );
</script>

Routes:

post 'Home/Index' => 'home#index'

Controller (Index action):

@tickets  = Choice.betable(current_user).where("choices.created_at > ?", Time.at(params[:after].to_i + 1).utc)

respond_to do |format|
  format.json { render :json => @tickets }
  format.html
end

Solution:

After a lot of trial and error I ended up using a solution, where I render the HTML in the controller instead of in the javescript code in the view. In that way I have access to the params I needed.

respond_to do |format|
  format.html      
  format.json do
    @html = render_to_string( :partial => '/choices/choice.html.erb', :locals => { :choice => @tickets} )
    render :json => { :success => true, :html => @html }
  end
end

Thanks to Chazt3n for the extended help :) Really appriciated it..

I believe you could send it via AJAX

Function sendAfter((string?) after){
 var settings = { after: after}
 $.ajax({
        url: '/Controller/Action',
        type: 'POST',
        dataType: 'json',
        data: settings,
        cache: true,
        success: function (data) {}
      });
}

This will get your data through to the controller at least Just call that function and send it your parameter wherever you want it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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