5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

小学生でもわかるRuby on Rails入門のメモ

Last updated at Posted at 2014-07-20

vim-railsが便利

cd ~/.vim
git clone https://github.com/tpope/vim-rails
echo "set runtimepath+=${HOME}/.vim/vim-rails/" >> ~/.vimrc
  • :Rでview<-->contollerを切り替えられる。
  • :Vview

パッケージの準備

apt-get install -y sqlite3 libsqlite3-dev zlib1g-dev
gem install rails
gem install rake
プロジェクトの作成
rails new twitter
cd twitter

users/show作成

viewとcontroller作成
rails g controller users \
 index show
3000ポート
rails s

get値の取得

config/routes.rb
get 'users/index'
get 'users/show'
get 'users/show/:username' => "users#show"
  • users/show/:username にアクセスしたら UsersControllerのshowを明示的に利用するという意味。
  • get値の参照 => params[:username]

DB

  • データベース : ノート
  • スキーマ   : 情報を入力するための共通の枠
DBの作成
rake db:create
  • db/development.sqlite3 が生成される。

migrate

rails g model user \
  name:string \
  username:string \
  location:string \
  about:text
sqlite3に反映
rake db:migrate

初期データ

db/seeds.rb
@user = User.new
@user.name = 'Ryo Suzuki'
@user.username = 'ryooopan'
@user.location = 'Kanagawa, Japan'
@user.about = 'Hello, I am Ryo. I am from database!'
@user.save

@user = User.new
@user.name = 'Shohei Aoki'
@user.username = 'moyahima'
@user.location = 'Tottori, Japan'
@user.about = 'Nice to meet you. I am from database!'
@user.save
初期データ投入
rake db:seed
検索
@user = User.find_by(:username => params[:username])
app/views/users/show.html.erb
<h1><%= @user[:name] %></h1>
<p><%= @user[:username] %></p>
<ul>
  <li>Location : <%= @user[:location] %></li>
  <li>About    : <%= @user[:about] %></li>
</ul>

twitterっぽいやつ

rails g

viewとcontoller作成
rails g controller tweets \
 index show new
model作成
rails g model tweet \
 title:string \
 content:text
rake db:migrate
app/views/tweets/index.html.erb
<a href="/tweets/new">投稿</a>

<% @tweets.each do |tweet| %>
  <h1><%= tweet.title %></h1>
  <p><%= tweet.content %></p>
<% end %>
app/views/tweets/new.html.erb
<%= form_for Tweet.new do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title %>
  <%= f.label :content %>
  <%= f.text_area :content %>
  <%= f.submit %>
<% end %>
config/routes.rb
get 'tweets/index'
get 'tweets/show'
get 'tweets/new'
post 'tweets' => "tweets#create"

データの保存

app/controllers/tweets_controller.rb
  def create
    @tweet = Tweet.new
    @tweet.title = params[:tweet][:title]
    @tweet.content = params[:tweet][:content]
    @tweet.save
    redirect_to '/tweets/index'
  end
  • postしたら /tweets/index に飛ぶ
class TweetsController < ApplicationController
  def index
    @tweets = Tweet.all
  end
...

別の書き方

config/routes.rb

resources :tweets
resources :users

保存時のエラー対応

app/controllers/application_controller.rb
  class ApplicationController < ActionController::Base
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception
  
+   before_filter do
+     resource = controller_name.singularize.to_sym
+     method = "#{resource}_params"
+     params[resource] &&= send(method) if respond_to?(method, true)
+   end
  end
app/controllers/tweets_controller.rb
  class TweetsController < ApplicationController

    def create
-     @tweet = Tweet.new
-     @tweet.title = params[:tweet][:title]
-     @tweet.content = params[:tweet][:content]
-     @tweet.save
-     params.permit!
+     @tweet = Tweet.create(params[:tweet])
      redirect_to '/tweets'
    end

+   private
+   def tweet_params
+     params.require(:tweet).permit(:title,:content)
+   end

  end
5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?