The One Guide To Set Up A Rails Application

Generating a New Rails Application

In the terminal:

rails new blog-flash

Set Your Domain Model and Build Schema

By this point, you should have your domain models & attributes planned. Now it’s time to set that all up.

In terminal: rails g migration create_coupons coupon_code:string store:string#Creates file under db/migrater#=> 20201117213427_create_coupons.rb
rails g migration add_year_to_planet year:integer
Rails g controller planets –-no-test-framework#Creates file under controller#=> planets_controller.rb
rails g model Planet –no-test-framework
rails g resource Account name:string payment_status:string --no-test-framework
  1. Model file: a file for you along with a named class inheriting from ApplicationRecord.
  2. Controller file: a controller file you’ll need under the Controllers folder. It will also create a class for you, pluralize your object name to follow restful convention, and inherit from ApplicationController.
  3. View folder: a view folder for you to add your HTML.erb page files. This will only create the folder for you- without any files.
  4. Resources: routes with all seven routes in the routes.rb file.

Build your pages

Building out your pages and logic will most likely require logic and code in your view file, controller file, routes, and model file. You’ll find yourself hopping between these files as you build out pages and features.

Index Page

Let’s do a “view all” feature here for articles we want to display for our blog app.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Show Page

Let’s build a show page to show an article in our database. Data will be passed on to this page to link the article in reference. This will normally be in the form of a Primary Key ID from your database.

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

New & Create

You’ll be needing this page for a user to input and create objects to persist in your database. Here we will be adding a new article to our database through the new page. For this data input we will be using a from_for. We will also be using a concept called strong params for added security and to maintain data integrity.

def new
@article = Article.new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to post_path(@post)
end

private

def post_params
params.require(:post).permit(:title, :description)
end
<%= form_for @article do |f| %>
<%= f.label 'Article Title' %><br>
<%= f.text_field :title %><br>

<%= f.label 'Article Description' %><br>
<%= f.text_area :description %><br>

<%= f.submit "Submit Article" %>
<% end %>
Rails.application.routes.draw doresources :articles, only: [:index, :show, :new, :create]end

Edit & Update

You’ll be needing this page for a user to edit & update objects in your database. Here we will be editing a saved article in our database through the edit page. For this edit input we will be also using a from_for. Just like the new & create, we will also be using a concept called strong params for added security and to maintain data integrity.

def edit
@article = Article.find(params[:id])
end
def update
@post = Post.find(params[:id])
@post.update(post_params)
redirect_to post_path(@post)
end

private

def post_params
params.require(:post).permit(:title, :description)
end
<%= form_for @article do |f| %>
<%= f.label 'Article Title' %><br>
<%= f.text_field :title %><br>

<%= f.label 'Article Description' %><br>
<%= f.text_area :description %><br>

<%= f.submit "Submit Article" %>
<% end %>
resources :articles, only: [:index, :show, :new, :create, :edit, :update]

I get techy on here. { firstName: “Kevin”, lastName: “Botero”, interests: [“tech”, “startups”] }

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