{"id":3993,"date":"2023-11-04T23:13:59","date_gmt":"2023-11-04T23:13:59","guid":{"rendered":"http:\/\/localhost:10003\/how-to-create-a-web-app-with-ruby-on-rails-and-bootstrap\/"},"modified":"2023-11-05T05:48:24","modified_gmt":"2023-11-05T05:48:24","slug":"how-to-create-a-web-app-with-ruby-on-rails-and-bootstrap","status":"publish","type":"post","link":"http:\/\/localhost:10003\/how-to-create-a-web-app-with-ruby-on-rails-and-bootstrap\/","title":{"rendered":"How to Create a Web App with Ruby on Rails and Bootstrap"},"content":{"rendered":"
In this tutorial, we will create a web application using Ruby on Rails and bootstrap. Ruby on Rails is a popular web application framework written in Ruby, while Bootstrap is a powerful CSS framework that helps build responsive and mobile-first web applications.<\/p>\n
By the end of this tutorial, you will have a basic web application that allows users to create, read, update, and delete records from a database. We will be using Ruby on Rails version 6 and Bootstrap version 4.<\/p>\n
To follow this tutorial, you will need the following:<\/p>\n
To begin, let’s create a new Rails application. Open your terminal and run the following command:<\/p>\n
$ rails new webapp\n<\/code><\/pre>\nThis will create a new directory named “webapp” with a basic Rails application structure.<\/p>\n
Navigate into the application directory:<\/p>\n
$ cd webapp\n<\/code><\/pre>\nStep 2: Set Up the Database<\/h2>\n
Next, we need to set up the database for our application. Ruby on Rails uses SQLite as the default database. Open the config\/database.yml<\/code> file and make sure your database configuration matches the following:<\/p>\ndefault: &default\n adapter: sqlite3\n pool: <%= ENV.fetch(\"RAILS_MAX_THREADS\") { 5 } %>\n timeout: 5000\n database: db\/development.sqlite3\n\ndevelopment:\n <<: *default\n<\/code><\/pre>\nSave the file and run the following command to create the database:<\/p>\n
$ rails db:create\n<\/code><\/pre>\nStep 3: Generate a Scaffold<\/h2>\n
Now, we will generate a scaffold for our application. A scaffold generates a set of views and controllers for a specific model. In this tutorial, we will create a simple blog application.<\/p>\n
Run the following command to generate the scaffold:<\/p>\n
$ rails generate scaffold Post title:string body:text\n<\/code><\/pre>\nThis will generate the necessary files and code for the blog application, including the database migration and model, controller, and view files.<\/p>\n
Apply the database migration using the following command:<\/p>\n
$ rails db:migrate\n<\/code><\/pre>\nStep 4: Add Bootstrap to the Application<\/h2>\n
To add Bootstrap to our application, we will use the bootstrap<\/code> gem. Open your Gemfile and add the following line:<\/p>\ngem 'bootstrap', '~> 4.0.0'\n<\/code><\/pre>\nSave the file and run the following command to install the gem:<\/p>\n
$ bundle install\n<\/code><\/pre>\nNext, let’s import the Bootstrap stylesheets and JavaScript files into our application.<\/p>\n
Open the app\/assets\/stylesheets\/application.css<\/code> file and add the following line at the top:<\/p>\n@import 'bootstrap';\n<\/code><\/pre>\nOpen the app\/assets\/javascripts\/application.js<\/code> file and add the following lines:<\/p>\n\/\/= require jquery3\n\/\/= require popper\n\/\/= require bootstrap\n<\/code><\/pre>\nStep 5: Customize the Scaffold Views<\/h2>\n
Now, let’s customize the scaffold views to use Bootstrap classes and styling.<\/p>\n
Open the app\/views\/posts\/index.html.erb<\/code> file and replace its contents with the following:<\/p>\n<h1 class=\"my-4\">Posts<\/h1>\n\n<table class=\"table\">\n <thead>\n <tr>\n <th>Title<\/th>\n <th>Body<\/th>\n <th colspan=\"3\"><\/th>\n <\/tr>\n <\/thead>\n\n <tbody>\n <% @posts.each do |post| %>\n <tr>\n <td><%= post.title %><\/td>\n <td><%= post.body %><\/td>\n <td><%= link_to 'Show', post %><\/td>\n <td><%= link_to 'Edit', edit_post_path(post) %><\/td>\n <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %><\/td>\n <\/tr>\n <% end %>\n <\/tbody>\n<\/table>\n\n<br>\n\n<%= link_to 'New Post', new_post_path, class: 'btn btn-primary' %>\n<\/code><\/pre>\nOpen the app\/views\/posts\/show.html.erb<\/code> file and replace its contents with the following:<\/p>\n<h1 class=\"my-4\"><%= @post.title %><\/h1>\n\n<p><%= @post.body %><\/p>\n\n<br>\n\n<%= link_to 'Edit', edit_post_path(@post), class: 'btn btn-primary' %>\n<%= link_to 'Back', posts_path, class: 'btn btn-secondary' %>\n<\/code><\/pre>\nOpen the app\/views\/posts\/_form.html.erb<\/code> file and replace its contents with the following:<\/p>\n<%= form_with(model: post, local: true) do |form| %>\n <% if post.errors.any? %>\n <div id=\"error_explanation\">\n <h2><%= pluralize(post.errors.count, \"error\") %> prohibited this post from being saved:<\/h2>\n\n <ul>\n <% post.errors.full_messages.each do |message| %>\n <li><%= message %><\/li>\n <% end %>\n <\/ul>\n <\/div>\n <% end %>\n\n <div class=\"form-group\">\n <%= form.label :title %>\n <%= form.text_field :title, class: 'form-control' %>\n <\/div>\n\n <div class=\"form-group\">\n <%= form.label :body %>\n <%= form.text_area :body, rows: 10, class: 'form-control' %>\n <\/div>\n\n <div class=\"actions\">\n <%= form.submit 'Save', class: 'btn btn-primary' %>\n <\/div>\n<% end %>\n<\/code><\/pre>\nStep 6: Start the Rails Server<\/h2>\n
Now, let’s start the Rails server and see our web application in action!<\/p>\n
Run the following command to start the server:<\/p>\n
$ rails server\n<\/code><\/pre>\nOpen your web browser and navigate to `http:\/\/localhost:3000\/posts`. You should see the blog application with a list of posts.<\/p>\n
Click on the “New Post” button to create a new post. Fill in the title and body of the post and click the “Save” button.<\/p>\n
You can also edit and delete existing posts using the respective links on the posts list page.<\/p>\n
Congratulations! You have successfully created a web application with Ruby on Rails and Bootstrap.<\/p>\n
Conclusion<\/h2>\n
In this tutorial, we learned how to create a web application using Ruby on Rails and Bootstrap. We started by setting up a new Rails application and generating a scaffold for a blog application.<\/p>\n
We then added Bootstrap to our application and customized the scaffold views to use Bootstrap styling and classes.<\/p>\n
Finally, we started the Rails server and tested our web application by creating, editing, and deleting posts.<\/p>\n
This is just the beginning of what you can do with Ruby on Rails and Bootstrap. Feel free to explore further and add more features to your web application.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this tutorial, we will create a web application using Ruby on Rails and bootstrap. Ruby on Rails is a popular web application framework written in Ruby, while Bootstrap is a powerful CSS framework that helps build responsive and mobile-first web applications. By the end of this tutorial, you will Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[754,747,750,748,752,753,746,751,749,49,745],"yoast_head":"\nHow to Create a Web App with Ruby on Rails and Bootstrap - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n