Ruby On Rails: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 94: Line 94:
   end
   end
end
end
</syntaxhighlight>
=Layouts=
We can add bootstrap to our project by going to app/views/layouts/application.html.erb and adding the min to it.
<syntaxhighlight lang="erb">
<!DOCTYPE html>
<html>
  <head>
    <title>HU</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
...   
  </head>
  <body>
    <%= yield %>
  </body>
</html>
</syntaxhighlight>
</syntaxhighlight>

Revision as of 03:09, 14 August 2020

Installing

Install Dependencies

sudo apt update
sudo apt install -y curl gnupg2 dirmngr git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

Install Node

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install -y nodejs

Install Yarn

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install -y  yarn

Install Ruby

Takes a while so might want to add --verbose

 cd
 git clone https://github.com/rbenv/rbenv.git ~/.rbenv
 echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
 echo 'eval "$(rbenv init -)"' >> ~/.bashrc
 exec $SHELL
 
 git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
 echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
 exec $SHELL
 
 rbenv install 2.7.1
 rbenv global 2.7.1
 gem install bundler

Install Rails

 gem install rails

Creating a Project

This took a while but did finish on version 6.0.3.2 of rails

# Create 
rails new HU
# Start the server http://localhost:3000
rails server # rails s

Basics

Creating a Controller

You pass a name and an action to create a controller

rails generate controller home index

We can not change the content of the page which is found at app/views/home/index.html.erb

<h1>Welcome</h1>

Changing the default route

Lets change the default route page to be the new page. Go app/config/routes.rb
Change from

Rails.application.routes.draw do
  get 'home#index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

Change to

Rails.application.routes.draw do
  root 'home#index'
  # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

Adding a new route

Add Route

In app/config/routes.rb add a new route by adding a get with route name and a controller and action

get '/about' => 'home#about'

Add View

You will need a new view under app/views called about.html.erb

<h1>Abouty<h1>

Add Action

You will need a new action under app/controller/home_controller.rb

class HomeController < ApplicationController
...
  def about
  end
end

Layouts

We can add bootstrap to our project by going to app/views/layouts/application.html.erb and adding the min to it.

<!DOCTYPE html>
<html>
  <head>
    <title>HU</title>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
...    
  </head>

  <body>
    <%= yield %>
  </body>
</html>