RubyMine: Debugging a Ruby on Rails Application

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the jetbrains youtube channel in this video we'll walk you through the main debugging capabilities of rubymine we'll be using a Ruby debugger to investigate the life cycle of a Ruby on Rails application from handling requests to saving data to the database in this tutorial we're using the micro-blogging rails application that allows users to create accounts and post messages you can clone this application using the link in the description before debugging the application make sure you've specified the Ruby interpreter and installed the gems in our application we're using a separate gem set first off we need to place breakpoints in our code execution of the application will be suspended at these points and we can examine the program state considering elements like variable values stack frames and so on open the routes dot RB file and set a breakpoint next to the resources method this method declares a standard rest resource and maps URLs to controller actions to handle requests then open users controller and place breakpoints within the new and create actions these actions are responsible for rendering a form and submitting the form data into the database respectively click the gutter icon next to the new action to open the corresponding view file will place the breakpoint next to the forum with helper method finally open the user dot RB model file and place a breakpoint within the down case email method this method converts the email address to lowercase before saving a user to the database now we are ready to start the debugging session for running and debugging rails applications rubymine automatically creates the development and production run configurations for these configurations you can specify various startup parameters such as the server type IP address and port rails environment and so on click the debug button on the toolbar to start the debugging session rubymine suggests installing the D base and Ruby debug IDE gems required for debugging after installing the gems you will be prompted to patch the spring configuration file this needs to be done to load the debugger and do every process forked by spring if necessary you can disable spring for the current debugging session and re-enable it later we'll choose to update the project configuration file the program will be stopped on the resources method call on the frames pane you can see the current frame stack where each frame corresponds to a method call each time a method is called a new frame is added to the top of the stack when the execution of a method is complete the corresponding frame is removed from the stack as you can see our rails application starts booting from the bin rails executable note that you can filter out the project and library frames using the filter button you can see in the top right-hand corner let's resume the programs execution by clicking the resume button the console tab shows that the rail server is started and is listening on the 3000 port open the application in the browser and click sign up now this time the debugging session will be suspended on the new action which handles rendering for the sign up page if you switch to the debugger tab you will see that the user variable is nil since it has not yet been initialized you can also see the action controller parameters here let's add them to watches to track their values throughout the application lifecycle if you click resume the application will be stopped in a view corresponding to the new action you can hover over the user variable on the editor to see that it is initialized now the action controller parameters haven't changed yet clicking the resume button again opens the signup page let's create a sample JetBrains user when you click create my account the debugging session is stopped in the create action the set form data can be fetched using action controller parameters let's step through the program step-over allows you to go to the next line while step into enables you to enter the methods invoked along the way click step into or press f7 rubymine suggests that you select between the user params and new methods select user params and press enter to jump to the method definition note that you can not only jump to project entities but also to definitions within external libraries enables showing external calls first and then press f7 choose the params actioncontroller method and press ENTER rubymine will open the method definition in the strong parameters module now go back to the line within the create action where the user is saved you can place the carrot here and click the run to cursor button to continue the execution until this line is reached the user variable is now initialized and uses attribute values received from the action controller parameters you can examine the user object right in the editor or you can do this in the interactive debugging console the console prompt can be disabled or enabled using a corresponding button on the toolbar let's take a look at the users email here if you click resume now rails begins an active record transaction responsible for saving a user the application stops on the down case email method which converts the email address to lowercase before saving the user to a database let's check the expression value in the console you can also evaluate this expression in the editor by selecting it and pressing alt command f8 on Mac OS or ctrl alt f8 on Windows or Linux click resume the user will be saved to the database let's take a look at the breakpoint options for each breakpoint you can configure additional properties you can enable or disable it specify a condition for hitting a breakpoint and so on the carrot is now placed at the break point within the down case email method you can open the break point properties by pressing shift command 8 on Mac OS or alt shift f8 on Windows or Linux pressing this shortcut again invokes the break points dialogue with all breakpoints set in our application let's disable all of them except the one within the create action the breakpoint has multiple options for example suspend pauses the program execution when a breakpoint is hit if necessary you can disable this checkbox to obtain logging information or calculate an expression at a certain point without interrupting the program for example you can use evaluate and log to evaluate an expression when the breakpoint is hit and show the result in the console output the condition option allows you to specify a condition for hitting a breakpoint let's enable it and add the condition where a username is equal to rubymine click done to save the settings you can see the breakpoint is marked with the question mark since it has a condition let's drag the breakpoint one line below now if you create a user whose name doesn't match the condition the breakpoint will not be hit however creating a user with the name rubymine suspends the application at the breakpoint let's look at another useful tip for working with break points imagine you've specified various properties of a break point and then accidentally removed it to restore it add a new break point at this line right click it and click restore previous break point the break point will be restored with all the original settings and that's it you can learn more about debugging from our documentation thanks for watching you
Info
Channel: JetBrainsTV
Views: 7,164
Rating: undefined out of 5
Keywords: JetBrains, software development, developer tools, programming, developer, rubymine, learn, debugging, pry, debug console, IDE, ruby-debug-ide, debase
Id: 8QXWPpuQe-Q
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Mon Jun 08 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.