menu

Rails - Make UI (landing page home) with javascript, erb, and sass


Melanjutkan tutorial kemarin tentang route, controller, view, tutorial kali ini akan membahas tentang membuat sebuah home page website pada Rails dengan menggunakan javascript, erb, dan sass.

Oke yang pertama silahkan generate terlebih dahulu controller home, caranya dengan mengetikkan code pada terminal ubuntu seperti ini,


Selanjutnya buat route baru untuk halaman home dengan mengedit file routes.rb pada path config/ menjadi seperti ini,
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  # root 'application#hello'
  get 'users' => 'users#index'
  get '' => 'home#index'
end

Kemudian, buat controller baru untuk menampilkan halaman index home dengan mengedit file home_controller.rb pada path app/controllers/ menjadi seperti ini,
class HomeController < ApplicationController
 def index

 end
end

Setelah itu edit file application.html.erb pada path app/views/layouts/ menjadi seperti ini,
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Page</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>
    <div class="header">
      <div class="content">
        <a href="/" class="button-header">Home</a>
        <div class="dropdown">
          <a class="button-header dropbtn" onclick="myFunction()">Table</a>
          <div id="myDropdown" class="dropdown-content">
            <a href="#">Link 1</a>
            <a href="#">Link 2</a>
            <a href="#">Link 3</a>
          </div>
        </div>
      </div>
    </div>
    <div class="isi">
      <%= yield %>
    </div>
    <div class="footer">
      COPYRIGHT © 2016 CODEDOCT.COM ALL RIGHTS RESERVED.
    </div>
  </body>
</html>

Selanjutnya, edit file home.scss pada path app/assets/stylesheets/ menjadi seperti ini,
// Place all the styles related to the home controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
body{
 margin: 0;
 padding: 0;
}

// Global css
.text-center{
 text-align: center;
}

.header{
 height: 50px;
 line-height: 50px;
 width: 100%;
 background-color: #1da1f2;
 position:fixed;
 top: 0;
 .content{
  padding: 0 50px;
  .button-header{
   text-decoration: none;
   padding: 15px 20px;
   &:hover{
    opacity: 0.1;
    cursor: pointer;
   }
  }
  .dropdown {
      position: relative;
      display: inline-block;
      .dropdown-content {
       display: none;
       position: absolute;
       background-color: #f9f9f9;
       min-width: 160px;
       overflow: auto;
       box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
       a{
        color: black;
        padding: 6px 16px;
        text-decoration: none;
        display: block;
        &:hover{
         background-color: #f1f1f1
        }
       }
   }
  }
 }
}
.isi{
 padding: 10px 20px;
 border: 1px solid #ddd;
 border-radius: 3px;
 width: 90%;
 margin: 100px auto; 
}
.footer {
 position:fixed;
 left:0px;
 bottom:0px;
 height:50px;
 width:100%;
 background:#fff;
 line-height: 50px;
 text-align: center;
 border-top: 1px solid #ddd;
}

Edit pula file home.js pada path app/assets/javascripts/ menjadi seperti ini,
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
function myFunction() {
  $('.dropdown-content').toggle('slow');
}

$(document).mouseup(function (e)
{
    var container = $('.dropdown-content');

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.hide();
    }
});

Terakhir buat file index.html.erb pada path app/views/home/ kemudian isikan code berikut ini,
<%= provide(:title, "Home") %>
<h1 class="text-center">Welcome to my ruby website</h1>
<br>
<p>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>

Sehingga hasilnya akan tampak seperti ini,



===DONE!===

Rails - routes, controller, view

 

Kembali lagi ke Ruby on Rails,..!!!

Karena tutorial PHP dan Laravel sudah sangat jauh, maka mulai saat ini codedoct akan lebih fokus pada tutorial ruby on rails,,

Pada tutorial sebelumnya kita sudah membuat sebuah project Rails dengan nama project-blog-ruby, kali ini pembelajaran akan lebih menitik beratkan pada pemahaman tentang routes, controller, dan view dari framework Rails ini.

Ok, jalankan terlebih dahulu project-blog-ruby dengan mengetikkan code rails server pada directory project-blog-ruby sehingga akan terlihat seperti ini,


Selanjutnya buat controller baru dengan nama Users dengan cara seperti ini,


Buka file routes.rb pada path config/ dan edit menjadi seperti ini,
Rails.application.routes.draw do
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'application#hello'
  get 'users' => 'users#index'
end

Kemudian, setelah generate controller, rails akan secara otomatis membuat file controller baru dengan nama users_controller.rb pada path app/controllers/ dan edit menjadi seperti ini,
class UsersController < ApplicationController
 def index
  @nama = ['Jhon', 'Garp', 'Tirex']
 end
end

Setelah itu, buka file application.html.erb pada path app/views/layouts/ file ini akan kita jadikan sebagai file layout master, edit menjadi seperti ini..
<!DOCTYPE html>
<html>
  <head>
    <title><%= yield(:title) %> | Page</title>
    <%= csrf_meta_tags %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

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

Terakhir buat file view baru dengan nama index.html.erb pada path app/views/users dan isikan code berikut,
<%= provide(:title, "User") %>
<div class="test">hai</div>
<% @nama.each do |x| %>
 <h4><%= x %></h4>
<% end %>

Jangan lupa untuk menambahkan css, anda bisa menambahkannya pada file users.scss yang terletak pada path app/assets/stylesheets/ file ini secara otomatis dibuat saat kita generate controller, edit file ini menjadi seperti ini,
// Place all the styles related to the Users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
.test{
 color: green;
}
lain halnya pada Laravel, pada Rails anda tidak perlu mengcompile file scss ini menjadi file css,

Sehingga hasilnya akan tampak seperti ini,



===DONE!===

How - fix error on github clone(permission denied)


Oke karena codedoct baru saja upgrade os Ubuntu ke versi 16.04 LTS maka untuk melanjutkan development project sebelumnya yang sudah ada di github dalam hal ini project-blog-ruby, kita harus meng-clone project tersebut ke komputer yang baru diinstall, caranya?..
# git clone 
sudo git clone https://github.com/codedoct/project-blog-ruby.git

#masuk ke path project-blog-ruby
cd project-blog-ruby

#bundle install rails
bundle install

#jika terjadi error#
An error occurred while installing pg (0.18.4), and Bundler cannot continue.
Make sure that `gem install pg -v '0.18.4'` succeeds before bundling.

#install libpq terlebih dahulu
sudo apt-get install libpq-dev

#bundle install lagi
bundle install


Oke, sekarang project sudah siap untuk di running
rails server

#jika terjadi error#
Permission denied @ dir_s_mkdir - /var/www/html/blog/project-blog-ruby/tmp/cache

#ubah saja owner folder seperti ini
#mundur satu path dir 
cd ..
sudo chown -R user_name:root project-blog-ruby

#kita juga bisa memperbaikinya dengan cara ini
sudo chmod -R 777 project-blog-ruby
#tapi cara chmod atau mengubah hak akses sangat tidak dianjurkan karena terlalu beresiko untuk project kita bisa di hack


Jika rails server sudah running dengan benar coba test dengan membuka url localhost:3000

===DONE!===

How - Install LAMP server di ubuntu 16.04 LTS



Hello everyone,,

Kali ini codedoct akan membagikan sedikit pengalaman tentang ubuntu 16.04.. yup, belum lama ini ubuntu telah merilis versi terbarunya yaitu 16.04 LTS codedoct pun tertarik untuk mencobanya untuk keperluan development,

Dan setelah ubuntu 16.04 LTS sudah terinstall pada tablet Acer Iconia W700 codedoct pun mencoba untuk menginstall beberapa aplikasi pendukung untuk development, dalam hal ini yaitu LAMP server, apa itu LAMP server? lihat disini.

Setelah mencoba menginstall LAMP server dengan cara ini, terjadi masalah serperti ini,

Pada saat codedoct mencoba untuk restart service apache2 terjadi error seperti ini,



Kemudian codedoct mencoba untuk membuat halaman php info pada dir /var/www/html/ tapi hasilnya blank, alias halaman php sama sekali tidak bisa di display atau hanya menampilkan layar putih,

Selanjutnya codedoctpun mencoba menginstall phpmyadmin kemudian mengetesnya dengan membuka url localhost/phpmyadmin hasilnya serperti ini,


Setelah mengetahui beberapa error yang ditunjukkan codedoct pun berasumsi ada masalah pada php yang telah terinstall, setelah mencari tahu sana-sini akhirnya codedoct banyak membaca artikel bahwa default php ubuntu 16.04 LTS adalah php7.0,

Kemudian codedoct mencoba untuk menginstall ulang LAMP server dengan mengikuti cara pada page website ini.

Dan hasilnya sama saja, php7.0 tidak bekerja dengan baik, oke selanjutnya codedoct mencoba untuk menginstall php5.6 dan juga php7.0 secara bersamaan dengan code seperti ini,
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0

Setelah itu menonaktifkan php7.0 dan mengaktifkan php5.6
sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart

Dan hasilnya tara......!!

it work,.,.,

Kemudian codedoct mencoba untuk menonaktifkan php5.6 dan mengaktifkan php7.0
sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart

Dan hasilnya tara.....!

it work to.,.,.

Akhirnya codedoct menyimpulkan bahwa untuk dapat untuk menjalankan php7.0 pada LAMP server ubuntu 16.04 LTS anda juga harus menginstall php5.6. Codedoct tidak tahu alasannya, dan codedoct juga tidak tahu hasilnya jika LAMP server ini diinstall pada ubuntu 16.04 LTS dengan menggunakan komputer tipe lain,, mungkin teman-teman ada yang sudah mencobanya?

===DONE!===

Syntax - Syntax sass


Sass merupakan salah satu compiler css yang sangat populer, untuk dapat menggunakan sass sebagai compiler css, kita harus menginstallnya terlebih dahulu, lihat disini.

Berikut beberapa syntax sass dan perbandingannya dengan css biasa,

Parent and child
//sass
.test{
    position: absolute;
    border: 4px solid black;
    top: 200px;
    left: 25%;
    width: 50%;
    height: 200px;
    text-align: center;
    span{
      font-size: 24px;
      line-height: 200px;
    }
}
//css
.test span {
  font-size: 24px;
  line-height: 200px;
}

Use one parameter
//sass
.test {
  font-family: 'Pacifico', cursive;
  height: 400px;
  background-image: url("lemonade.jpg");
  border:{
    top: 4px solid black;
    bottom: 4px solid black;
  }
}
//css
.test {
  font-family: 'Pacifico', cursive;
  height: 400px;
  background-image: url("lemonade.jpg");
  border-top: 4px solid black;
  border-bottom: 4px solid black;
}

Use variable
//sass
$translucent-white:rgba(255,255,255,0.3);
$standard-border: 4px solid black;
.test{
 background-color: $translucent-white;
 border: $standard-border;
}
//css
.test{
 background-color: rgba(255, 255, 255, 0.3);
 border: 4px solid black;
}

Use mixin
//sass
@mixin transform($transformation) {
  transform: $transformation;
  -webkit-transform: $transformation;
  -moz-transform: $transformation;
  -ms-transform: $transformation;
  -o-transform: $transformation;  
}
.test {
 width: 300px;
 height: 180px;
 &:hover{
  @include transform (rotatey(-180deg));  
 } 
}
//css
.test {
  width: 300px;
  height: 180px;
}
.test:hover {
  transform: rotatey(-180deg);
  -webkit-transform: rotatey(-180deg);
  -moz-transform: rotatey(-180deg);
  -ms-transform: rotatey(-180deg);
  -o-transform: rotatey(-180deg);
}

Variable on mixin
//sass
$stripe-properties: to bottom, 15%, blue, white;
@mixin stripes($direction, $width-percent, $stripe-color, $stripe-background: #FFF) {
  background: repeating-linear-gradient(
    $direction,
    $stripe-background,
    $stripe-background ($width-percent - 1),
    $stripe-color 1%,
    $stripe-background $width-percent
  );
}
.test {
 width: 100%;
 height: 100%;
 @include stripes($stripe-properties...);
}
//css
.test {
  width: 100%;
  height: 100%;
  background: repeating-linear-gradient(to bottom, white, white 14%, blue 1%, white 15%);
}

Use String
//sass
@mixin photo-content($file) {
  content: url(#{$file}.jpg); //this is ruby???
  object-fit: cover;
}
.image {
    @include photo-content('titanosaur');
    width: 60%;
    margin: 0px auto;  
}
//css
.image {
  width: 60%;
  margin: 0px auto;
  object-fit: cover;
  content: url(titanosaur.jpg);
}

Hover in mixin
//sass
@mixin hover-color($color) {
   &:hover{
       color: $color;
   }
}
.test {
 display: block;
 text-align: center;
 position: relative;
 top: 40%;
 @include hover-color(red);
}    
//css
.test:hover {
  color: red;
}

You can use your math skill
//sass
.test {
  color: red+blue;
}
//css
.math {
  color: magenta; //hahahaha
}

//other math
$width:250px;
.math {
  width: $width;
  border-radius: $width/6;
}
//css
.math{
  width:250px;
  border-radius: 41.66667px;
}

Looping
//use each
$list: (orange, purple, teal);
@each $item in $list { //shit this code same with ruby hahaha,.,.
  .#{$item} {
    background: $item;
  }
}
//css
.orange {
  background: orange;
}
.purple {
  background: purple;
}
.teal {
  background: teal;
}

//use for
$total: 10;
$step: 360deg / $total;
.test {
  height: 30px;
}
@for $i from 1 through $total {
  .test:nth-child(#{$i}) {
    background: adjust-hue(blue, $i * $step);
   }
}
//css
.test {
  height: 30px;
}
.test:nth-child(1) {
  background: #9900ff;
}
.test:nth-child(2) {
  background: #ff00cc;
}
.test:nth-child(3) {
  background: #ff0033;
}
.test:nth-child(4) {
  background: #ff6600;
}
.test:nth-child(5) {
  background: yellow;
}
.test:nth-child(6) {
  background: #66ff00;
}
.test:nth-child(7) {
  background: #00ff33;
}
.test:nth-child(8) {
  background: #00ffcc;
}
.test:nth-child(9) {
  background: #0099ff;
}
.test:nth-child(10) {
  background: blue;
}

Import scss
//import other scss files
@import "test";

//extend
.test{
 position: absolute;
}
.test2 {
    @extend .absolute;
}
//css
.test, .test2, {
  position: absolute;
}

Syntax - Syntax php


Artikel kali ini akan membahas beberapa syntax yang sering digunakan dalam programming PHP.

Tag And comment
===> tag php
<?php ?>

<?php

//===> comment one line
// content comment

//===> comment multi line
/*  
 content comment
 content comment
*/

Display
//===> show string
echo "content string";

//===> show integer
echo 21;

//===> show variabel
echo $variabel;

//===> mix variabel and string
echo "test ".$variabel." test2";

Array
here

Loop
//===> for
for ($i = 2; $i < 11; $i = $i + 2) {
  echo $i;
}
output:
246810

//===> foreach
$sentence = array("Im ", "learning ", "PHP!");        
foreach($sentence as $word) {
  echo $word;
}
output:
Im learning PHP!

//===> while
$loopCond = true;
while ($loopCond){
 echo "<p>The loop is running.<p>";
 $loopCond = false;
}
output:
The loop is running.
//other example
$i=0;
while($i<5){
    echo $i;
    $i++;
}
output:
01234
//with other way
$i=0;
while($i<5):
    echo $i;
    $i++;
endwhile;
output:
01234

//===> do while
$loopCond = false;
do {
 echo "<p>The loop ran even though the loop condition is false.</p>";
} while ($loopCond);
output:
The loop ran even though the loop condition is false.

Library
//===> length
$length = strlen("sayah");
echo $length;
output:
5

//===> partial, uppercase, lowercase
$myname = "Dark King";

$partial = substr($myname, 1, 3);
print $partial;

$uppercase = strtoupper($myname);
print $uppercase;

$lowercase = strtolower($myname);
print $lowercase;

output:
ark
DARK KING
dark king

//===> find position
$myname = "Dark King";
echo strpos($myname, "i");
output:
6

//===> round(pembulatan)
$round = round(M_PI);
print $round;

$round = round(M_PI, 3);
print $round;
output:
3
3.142

//===> rand(random number between)
 print rand(1, 10);

$name = "Dark King";
echo $name[rand(0, strlen($name)-1)];
output:
4
n

//===> length string
$name = "Dark King";
echo strlen($name);
output:
9

Function
//===> play with function
function greetings($name){
    echo "Greetings, " . $name . "!";
}

greetings("Dark King");
output:
Greetings, Dark King!

//===> class
// The code below creates the class
class Person {
    // Creating some properties (variables tied to an object)
    public $isAlive = true;
    public $firstname;
    public $lastname;
    public $age;
    
    // Assigning the values
    public function __construct($firstname, $lastname, $age) {
      $this->firstname = $firstname;
      $this->lastname = $lastname;
      $this->age = $age;
    }
    
    // Creating a method (function tied to an object)
    public function greet() {
      return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
    }
  }
  
// Creating a new person called "boring 12345", who is 12345 years old ;-)
$me = new Person('Dark', 'King', 12345);

// Printing out, what the greet method returns
echo $me->greet(); 
output:
Hello, my name is Dark King Nice to meet you!

//===> other example class
class Dog{
    public $numLegs = 4;
    public $name;
    
    public function __construct($name){
        $this->name = $name;    
    }
    
    public function bark(){
        return "Woof!";   
    }
    
    public function greet(){
        return "My name is ".$this->name;    
    }
}

$dog1 = new Dog("Barker");
$dog2 = new Dog("Amigo");

echo $dog1->bark();
echo $dog2->greet();
output:
Woof! My name is Amigo

//===> cheking class
class Person {
  public $isAlive = true;
  
  function __construct($name) {
      $this->name = $name;
  }
  
  public function dance() {
    return "I'm dancing!";
  }
}

$me = new Person("Shane");
if (is_a($me, "Person")) {
  echo "I'm a person, ";
}
if (property_exists($me, "name")) {
  echo "I have a name, ";
}
if (method_exists($me, "dance")) {
  echo "and I know how to dance!";
}
output:
I m a person, I have a name, and I know how to dance!

//===> cheking class
class Shape {
  public $hasSides = true;
}
class Square extends Shape {

}
$square = new Square();
if (property_exists($square, 'hasSides')) {
  echo "I have sides!";
}
output:
I have sides!

//===> overide class / extends
class Vehicle {
  public function honk() {
    return "HONK HONK!";
  }
}
class Bicycle extends Vehicle {
   public function honk() {
      return "Beep beep!";   
   }
}
$bicycle = new Bicycle();
echo $bicycle->honk();
output:
Beep beep!

//===> call const
class Ninja extends Person {
  const stealth = "MAXIMUM";
}
echo Ninja::stealth;
output:
MAXIMUM

//===> const
class King {
  public static function proclaim() {
    echo "A kingly proclamation!";
  }
}
King::proclaim()
output:
A kingly proclamation!

How - Use Charles Proxy (Mobile App)


On the previous trick we have run Charles Proxy on web browser, this time we will join Charles Proxy with your device(mobilephone), for get the url of mobile app.

Ok just following the tutorial,

The first time, you must install Certificates Charles Proxy on your mobilephone, you can download here,

If done, then open that certificates on your mobilephone, so it would appear like this,


Fill the name certificates and "Oke"

Next, set the manual proxy on your mobilephone like this,




Fill the field Proxy with ip on your computer and Port field with your setting on Charles Proxy,



In the picture above, proxy settings done by equating the settings on your computer, where ip computer can be found on Charles Proxy




or command prompt (type ipconfig on command prompt see on IPv4 address)


Whereas port proxy, you can set port proxy on your Charles Proxy like this,
Just click menu Proxy and click Proxy Settings


And then set the Port,



The last, run Charles Proxy and then open one of your app on mobile phone, so that the view on Charles Proxy will look like this,



===DONE!===