Rails 3: links that trigger actions but don't leave / refresh the current page?

Is there a way in Rails 3 to invoke an action (to be played using the database) without updating or triggering the action and then redirecting to the current page?

I tried messing with :remote => truein link_to, but this does not work for me. Or maybe I misunderstood its purpose ... or I use it incorrectly.

+5
source share
2 answers

There are two scenarios that you usually use when invoking an action from a view in Rails3:

  • Using the usual link_towith its options.
  • Use link_towith optional argument :remote => true(default - false)

/:

  • .
  • (render redirect), , . , do_db_stuff, Rails3 do_db_stuff.html.erg ( do_db_stuff.html.haml`) .
  • , render redirect :
    • render: , .
    • redirect: ( ) .

, , create some save method, , , render :action => "new" # doen't execute the method

. . , , - :

  • .
  • , search_task ( JavaScript AJAX) .
  • , render :partial ....
  • JavaScript , :

    element.bind('click', function() {
        $('#left').children().removeClass('selected')
        element.addClass('selected');
        $.ajax({url: '/tasks/search_task/' + task_id, 
            async: false,
            dataType: 'html',
            success: function(data) {
                target.html(data);
            }});
    }
    

    ( )

  • , ( ) , target HTML-.

, - link_to, , .

, , :remote => true , .

+6

AJAX :

<%= link_to "stock", { :controller => 'ReadItLater', :action => "stock", 
                     :qid => q.question_id } , :remote => true, :class=>'ddm' %>

:

    def stock
      @user = UserData.find_by_login(session[:cuser])
      @user.stock(params)

      respond_to do |format|
          format.js { render :layout=>false }
      end
  end
+2

All Articles