LoginSignup
1
0

Ruby on Railsでアプリを作成してみた【掲示板アプリ】

Last updated at Posted at 2023-05-28

1.プロジェクトを作成する

rails new <プロジェクト名>
rails server

image.png

2.ルーティングを作成する

config/routes.rb
Rails.application.routes.draw do

  # localhost:3000/posts

  resources :posts,only: [:index, :new, :create, :edit, :update, :destroy]
end

3.コントローラを作成する

rails generate controller <プロジェクト名>
controllers/posts_controller.rb
class PostsController < ApplicationController
    def index
    end
end

4.ビューを作成する

① viewsのpostsファイルの中に、index.html.erbを作成する。

views/posts/index.html.erb
<h1>Hello Ruby</h1>

<div class="button-container">
<%= link_to "新しい投稿", new_post_path, class:"link-button" %>
</div>

<ul>
<% @posts.each do |post|%>
<li>
<h2><%= post.title %></h2>
<p><%= post.content %></p>
<span><%= post.created_at %></span>
<div class="edit-delete-wrapper">
<%= link_to "編集", edit_post_path(post), class:"link-button" %>
<%= button_to "削除", post_path(post), method: :delete, class:"link-delete-button" %>
</div>
</li>
<% end %>
</ul>

② viewsのpostsファイルの中に、new.html.erbを作成する。

views/posts/new.html.erb
<h1>新しい投稿</h1>

<%= form_with model: @posts,html:{class: "form-container"} do |form| %>
<div class="form-input">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="form-input">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="button-container">
<%= form.submit "投稿", class:"link-button"%>
</div>
<% end %>

③ viewsのpostsファイルの中に、edit.html.erbを作成する。

views/posts/edit.html.erb

<h1>投稿編集</h1>

<%= form_with model: @post,html:{class: "form-container"} do |form| %>
<div class="form-input">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="form-input">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="button-container">
<%= form.submit "更新", class:"link-button"%>
</div>
<% end %>

5.モデルを作成する

rails generate model Posts title:string content:text
rails db:migrate

6.コントローラを編集する

controllers/posts_controller.rb
class PostsController < ApplicationController
    before_action :set_post, only:[:edit, :update, :destroy]
    
    # 投稿された内容を全て取得する
    def index
        # データベースから投稿を全て取得する
        @posts = Post.all
    end

    # 新しい投稿のコントローラ設定
    def new
        # データベースから投稿を全て取得する
        @post = Post.new
    end

    def create
        @post = Post.new(post_params)

        if @post.save
            redirect_to posts_path
        else
            render :new
        end
    end

    def edit
        # @post = Post.find(params:[:id])
    end

    def update
        # @post = Post.find(params:[:id])
        if @post.update(post_params)
            redirect_to posts_path
        else
            render :edit
        end    
    end

    def destroy
        # @post = Post.find(params:[:id])
        @post.destroy
        redirect_to posts_path
    end

    private
    def post_params
        params.require(:post).permit(:title,:content)
    end

    def set_post
        @post = Post.find(params:[:id])
    end
end

7.CSSを作成する

app/assets/stylesheets/application.css
body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
  background-color: #f8f9fa;
}

small {
  color: gray;
}

h1 {
  color: #343a40;
  text-align: center;
  padding: 20px 0;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0 30%;
  margin-top: 30px;
}

ul li {
  background-color: #fff;
  border: 1px solid #ced4da;
  margin-bottom: 10px;
  padding: 20px;
}

ul li h2 {
  margin: 0 0 10px 0;
  color: #495057;
}

ul li p {
  color: #212529;
  margin: 0;
}

.button-container {
  text-align: center;
}

.form-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #fff;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0px 0px 10px 0px rgba(0, 0, 0, 0.15);
  max-width: 500px;
  margin: auto;
  margin-top: 50px;
}

.form-input {
  margin-bottom: 20px;
  width: 100%;
}

.form-input input,
.form-input textarea {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}

.link-button {
  display: inline-block;
  background-color: #007bff;
  color: #fff;
  padding: 10px 20px;
  text-decoration: none;
  border-radius: 5px;
  text-align: center;
  cursor: pointer;
}

.link-delete-button {
  display: inline-block;
  background-color: #cc361f;
  font-size: 17px;
  color: #fff;
  padding: 10px 20px;
  text-decoration: none;
  border-radius: 5px;
  text-align: center;
  margin-left: 10px;
  border: 1px solid;
  cursor: pointer;
}

.edit-delete-wrapper {
  display: flex;
  margin-top: 10px;
}

参考サイト

【Ruby on Rails入門】初心者OK!掲示板アプリを作りながら学ぶRuby on Rails入門
【初心者向け】RubyonRailsで学ぶMVC入門
Railsの一連の流れをMVC,ルーティングの観点からまとめてみた

1
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
1
0