Ruby on Rails - Rails Save Record into Database - ruby on rails tutorial - rails guides - rails tutorial - ruby rails



What is a Database?

  • Database is a systematic collection of data.
  • Databases support storage and manipulation of data. Databases make data management easy.
  • Data is organized into rows, columns and tables, and it is indexed to make it easier to find relevant information.

How to Save Record into Database ?

  • We can save data into database using Ruby on Rails.
  • Let's see an example to save data of a form into database.

Step 1 : Create a new Rails application

rails new save record  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 2 : Change your directory to login.

cd save record  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 3 : Create scaffold from the console.

 rails generate scaffold User name:string password:digest  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 4 : Migrate your database.

rake db:migrate  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 5 : Go to Gemfile and uncomment the following line.

gem 'bcrypt'  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 6 : Run the following command:

           bundle install  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 7 : Go to app/controllers/users_controller.rb and write the following code in create method.

def create   
    @user = User.new(user_params)   
  
    respond_to do |format|   
      if @user.save   
        format.html { redirect_to users_url, notice: 'User was successfully saved.' }   
        format.json { render :show, status: :created, location: @user }   
      else   
        format.html { render :new }   
        format.json { render json: @user.errors, status: :unprocessable_entity }   
      end   
    end   
  end  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 8 : Go to app/views/users/index.html.erb file.

<h1>Listing Users</h1>   
  
<% if notice %>   
<p id="notice"> <%= notice %>   
</p>  
<%= link_to 'New User', new_user_path %>  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 9 : Go to app/views/users/new.html.erb file.

<h1>New User</h1>   
  
<%= render 'form', user: @user %>   
  
<%= link_to 'Back', users_path %>   
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 10 : Go to app/views/users/_form.html.erb file.

<%= form_for(user) do |f| %>   
  <% if user.errors.any? %>   
    <div id="error_explanation">   
      <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>   
  
      <ul>   
      <% user.errors.full_messages.each do |message| %>   
        <li><%= message %></li>   
      <% end %>   
      </ul>   
    </div>   
  <% end %>   
  
  <div class="field">   
    <%= f.label :name %>   
    <%= f.text_field :name %>   
  </div>   
  
  <div class="field">   
    <%= f.label :password %>   
    <%= f.password_field :password %>   
  </div>   
  
  <div class="field">   
    <%= f.label :password_confirmation %>   
    <%= f.password_field :password_confirmation %>   
  </div>   
  
  <div class="actions">   
    <%= f.submit %>   
  </div>   
<% end %>   
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 11 : Start the Rails server.

       rails s  
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Step 12 : Run it on localhost.

  • 1. http://localhost:3000/users
Ruby On Rails Save Data1
Learn Ruby On Rails Tutorials - Ruby On Rails Save Data1 - Ruby On Rails Examples
  • Click on New Use to add a new user.
Ruby On Rails Save Data2
Learn Ruby On Rails Tutorials - Ruby On Rails Save Data2 - Ruby On Rails Examples
  • Click on Create Use to add a new user.
Ruby On Rails Save Data3
Learn Ruby On Rails Tutorials - Ruby On Rails Save Data3 - Ruby On Rails Examples

This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Rails Save Record into Database