![]() |
In Ruby on Rails, there are several ways to add images to your application. In this article, we will see one of the common methods which is using an assets pipeline. Steps on How to Create a ProjectStep 1: Create a demo project using the command below. It will create a project named ‘myapp’ in the current directory. Then use the second command to get into your project directory.
![]() Create a Project Step 2: Now, we will create a controller and view using the following command. This command creates a controller file named ‘images_controller.rb’ in ‘app/controllers’ and its view file ‘index.html.erb’ in ‘app/views/home’. This command also creates a route file ‘config/routes.rb’.
![]() Create Controller and View Step 3: To display our index page, make changes in the ‘config/routes.rb’ file. Adding the following line in the file will set our index page as root.
Steps to Add Images Using AssetsStep 1: Now, we’ll add an image to the project. You can use any image you want. For example, let’s say we have an image named ‘GeeksforGeeks.png’. Place this image file in the ‘app/assets/images’ directory of your Rails project. ![]() Add Image to Path Step 2: To display the image, Open the ‘app/views/images/index.html.erb’ file and add the following lines.
Step 3: Start the server using below command and open “http://localhost:3000/” in your browser.
Output: ![]() Output |
Reffered: https://www.geeksforgeeks.org
Ruby |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |