2

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..

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

26 Comments

the after on the right side of the ":" is your after, the first is the name of the object you'll send as a param to the action.
Could you combine your code with my code, cause I don't know were to place your code within the code I have :) Thanks!
Sure thing, when is your code executed? is it on a click event?
I'm using Pusher (websocket) to trigger the event. This is triggered just fine, but I can't seem to get the after param along to to controller.
I've turned it into a function, try placing that as soon as you want that param sent in and it should work just fine. now is :After a string or an object? I'm sorry I'm half working and half trying to be helpful at the moment, I'll have more time after 5 here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.