ruby on rails examples

Ruby on Rails is a popular web development framework written in the Ruby programming language. It follows the Model-View-Controller (MVC) architectural pattern and provides a set of conventions to make web development faster and easier. Here are some examples of Ruby on Rails code snippets along with explanations for each step:

  1. Creating a new Rails application:
  2. Open your terminal and navigate to the desired directory.
  3. Run the command rails new myapp to create a new Rails application named "myapp".
  4. This command generates the basic structure and files needed for a Rails application.

  5. Generating a new model:

  6. Run the command rails generate model User name:string email:string to generate a new model named "User" with two attributes: "name" and "email".
  7. This command creates a migration file in the db/migrate directory, which defines the database schema for the "users" table.

  8. Running database migrations:

  9. Run the command rails db:migrate to apply pending database migrations.
  10. This command creates the "users" table in the database based on the migration file generated in the previous step.

  11. Creating a new controller:

  12. Run the command rails generate controller Users index show to generate a new controller named "Users" with two actions: "index" and "show".
  13. This command creates a new file app/controllers/users_controller.rb which contains the actions for handling requests related to users.

  14. Defining actions in the controller:

  15. Open the file app/controllers/users_controller.rb.
  16. Inside the UsersController class, define the actions: ```ruby def index @users = User.all end

    def show @user = User.find(params[:id]) end `` - Theindexaction fetches all the users from the database and assigns them to the instance variable@users. - Theshowaction finds a user by theidparameter passed in the request and assigns it to the instance variable@user`.

  17. Creating views:

  18. Create a new folder app/views/users if it doesn't already exist.
  19. Inside the app/views/users folder, create two files: index.html.erb and show.html.erb.
  20. In the index.html.erb file, you can display the list of users using the @users instance variable.
  21. In the show.html.erb file, you can display the details of a specific user using the @user instance variable.

  22. Routing requests:

  23. Open the file config/routes.rb.
  24. Add the following lines to define the routes for the UsersController actions: ruby get '/users', to: 'users#index' get '/users/:id', to: 'users#show'
  25. These lines map the URLs /users and /users/:id to the index and show actions of the UsersController, respectively.

  26. Starting the Rails server:

  27. Run the command rails server or rails s to start the Rails server.
  28. This command starts the server and makes your Rails application accessible at http://localhost:3000.

These are just a few examples of using Ruby on Rails. The framework provides many more features and functionalities for building robust web applications.