CRUD

Almost all web applications involve CRUD (Create, Read, Update, and Delete) operations. You may even find that the majority of the work your application does is CRUD. Rails acknowledge this and provide many features to help simplify code doing CRUD.

  • Create
  • Read
  • Update
  • Delete

Here is how to create a Rails CRUD application:

After creating a rails new app, change your directory to access the application. Generate the model for the application (model should be singular, do you remember?).

rails g model propertie title photo_url

We gonna get 2 files:

  • app/models/propertie.rb
  • db/migrate/[date_time]_create_propertie.rb (database will have 2 fields: title and photo_url.)

The next step is to generate a controller for the app

rails g controller propierties index show new edit

We gonna get the “properties_controller.rb” that gets created, a “properties” folder and files we requested gets added within the views folder:

  • index.html.erb
  • show.html.erb
  • new.html.erb
  • edit.html.erb

At that point check your config/routes.rb. The file should be automatically updated, but since we gonna use entire CRUD operations, le’s use the shorter version Rails is givin us:

Rails.application.rutes.draw do
resources :properties
end

Check your migration file, and if everything is looking good migrate the data.
To be able to move forward we need to create dome data, so go ahead to the db/migrate/seeds.rb:

Propertie.create(title: "VillaVienna", photo_url: "https://www.wien.info/media/images/klimt-villa-nordfassade-19to1.jpeg")

Run rake db:seed to add the properties to the database.

To make sure that everything was added correctly you can run test in console.
Run rails routes or go to localhost:3000/rails/info/routes to see all routes your application is configured to. Now we can start working on CRUD.

in app/controllers/properties_controller.rb, READ:
def index
@properties = Propertie.all
end
def show
@propertie = Propertie.find(params[:id])
end
CREATE:
def new
@propertie = Propertie.new
end
def create
propertie = Propertie.create(propertie_params)
redirect_to propertie_path
end
UPDATE:
def edit
@propertie = Propertie.find(params[:id])
end
def update
@propertie = Propertie.find(params[:id])
@propertie.update(propertie_params)
redirect_to propertie_path(@propertie)
end
DELETE:
def DESTROY
@propertie = Propertie.find(params[:id])
@propertie.destroy
redirect_to propertie_path
end
privatedef propertie_params
params.require(:propertie).permit(:title, :photo_url)
end
views/properties/index.html.erb,<ul>
<% @properties.each do |propertie| %>
<li> <%= link_to propertie.name, property_path(propertie) %>
</li>
<% end %>
</ul>
views/properties/show.html.erb<h1> <%= @propertie.title %> <h1>
<%= img src=@propertie.photo_url %>
<%= link_to 'Remove', @propertie, method: :delete, data: { confirm: 'Are you sure?' } %>views/properties/new.html.erb<h3> Create Propertie </h3>
<%= form_with model: @propertie do |form| %>
<%= form.text_field :title, placeholder: 'title' %>
<%= form.text_field :photo_url, placeholder: 'photo url' %>

<%= form.submit %>
<% end %>
views/properties/edit.html.erb<h3> Update Propertie </h3>
<%= form_with model: @propertie do |form| %>
<%= form.text_field :title, placeholder: 'title' %>
<%= form.text_field :photo_url, placeholder: 'photo url' %>

<%= form.submit %>
<% end %>

SOFTWARE ENGINEER — WEB DEVELOPER WITH HEART FOR FRONT-END, MINIMALISM AND RASMENTALISM.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store