Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Invoking controller/view methods from the console in Rails

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 162
    Comment on it

    At the time of development of a module or functionality we always curious that the code we wrote will work perfectly or not until and unless we run that in a browser. so rails provide us a way where we can test the functions via a console. By running rails console you will enter into an interactive environment where you can access the code of your rails application. and the environment is really helpful. It is commonly used where we have to see the data without logging to the database. you can also specify the rails environment like

    rails c RAILS_ENV=production 
    

    This is useful to troubleshoot things on the spot.

    Controller functions to be called like this:

    class DummyController < ApplicationController
    
      def test
        puts 'Hi there'
      end
    
    end
    
    1) controller = DummyController.new <RETURN>
    
    2) controller.test <RETURN>
    

    View Helpers functions to be called like this:

    include EventsHelper
    
    EventsHelper.display_event_image(238)
    

    or

    EventsController.helpers.display_event_image(238)
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: