LoginSignup
0
0

More than 3 years have passed since last update.

Ruby on Railsの基礎知識

Posted at

Railsを使う準備

新規プロジェクトの作成

terminal
$ rails new (プロジェクト名)

Webサーバーの起動(仮想環境)

terminal
$ rails s -b 0.0.0.0

※環境により異なります

掲示板の自動生成

terminal
$ rails generate scaffold article content:string

データベースの設定

terminal
$ rails db:migrate

Webページの追加(Welcomeというページを作成する場合)

terminal
$ rails generate controller welcome index

Welcomeページをトップページに設定する

config/routes.rb
Rails.application.routes.draw do
  get 'welcome/index'

  resources :articles
  root 'welcome#index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

リンクの設定

<%= link_to '(リンクテキスト)', リンクのアドレス(下記の場合は作成したarticlesに移動する) %>

index.html.erb
<%= link_to 'Show list', articles_path %>

データベースのarticlesテーブルに、nameカラムを追加する

terminal
$ rails generate migration AdNameToArticle name:string

$ rails db:migrateを忘れずに行います。

下記のようにコードを記述することでnameカラムからデータを呼び出す処理が実行できます。

<%= article.name %>
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0