Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4 | 5 | 6|Last
Lessons:- A Working Follow Button with Ajax

The code in Listing 11.37 is our first example of a controller test, which I used to use extensively (as in the previous edition of this book) but now mainly eschew in favor of integration tests. In this case, though, the xhr method is (somewhat inexplicably) not available in integration tests. Although our use of xhr is new, at this point in the tutorial you should be able to infer from context what the code does:

xhr :post, :create, relationship: {  followed id: other user.id }

 

We see that xhr takes as arguments a symbol for the relevant HTTP method, a symbol for the action, and a hash representing the contents of params in the controller itself. As in previous examples, we use expect to wrap the operation in a block and test for an increment or decrement in the relevant count.  

        As implied by the tests, the application code uses the same create and destroy actions to respond to the Ajax requests that it uses to respond to ordinary POST and DELETE HTTP requests. All we need to do is respond to a normal HTTP request with a redirect (as in Section 11.2.4) and respond to an Ajax request with JavaScript. The controller code appears as in Listing 11.38. (See Section 11.5 for an exercise showing an even more compact way to accomplish the same thing.)

 

 
 
 
image
Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student
Explore
 

  Students (0)