LoginSignup
0
0

More than 3 years have passed since last update.

Ruby on Rails [学習記録-第12章-]

Posted at

テーブルから不要なカラムを削除

  • テーブルからカラムを削除するためのマイグレーションファイルを作成して、カラムの削除を実行。
$ rails g migration Removeカラム名From削除元テーブル名 削除するカラム名:型
$ rails g migration RemoveNameFromTweets name:string
# マイグレーションファイルの作成、string型のnameというカラムを削除

$ rake db:migrate
# マイグレーションの実行

投稿内容を削除する

  • deleteメソッドは情報の削除を行う際に利用するHTTPメソッド。
  • 記事削除を行う際には、tweetsコントローラのdestroyアクションを動かします。
route.rb
Rails.application.routes.draw do
  devise_for :users
  root  'apps#index'
  get   'apps'      =>  'apps#index'
  get   'apps/new'  =>  'apps#new'
  post  'apps'      =>  'apps#create'
  delete  'apps/:id'  => 'apps#destroy'
  get   'users/:id'  =>  'apps#show'
end
 <div class="contents row">
  <% @apps.each do |app| %>
    <div class="content_post" style="background-image: url(<%= tweet.image %>);">
      <% if user_signed_in? && current_user.id == tweet.user_id %>
        <div class="more">
          <span><%= image_tag 'arrow_top.png' %></span>
          <ul class="more_list">
            <li>
              <%= link_to '削除', "/apps/#{app.id}", method: :delete %>
            </li>
          </ul>
        </div>
      <% end %>
        <%= simple_format(app.text) %>
      <span class="name">
        <a href="">
          <span>投稿者</span><%= app.user.nickname %>
        </a>
      </span>
    </div>
  <% end %>
  <%= paginate(@tweets) %>
</div>

destroyメソッド

  • ActiveRecordメソッドのうちの一つで、インスタンスに対してそのレコードを削除する際に使うことができる。
class AppsController < ApplicationController

  before_action :move_to_index, except: :index

  def index
    @apps = App.includes(:user).page(params[:page]).per(5).order("created_at DESC")
  end

  def new
  end

  def create
    App.create(image: app_params[:image], text: app_params[:text], user_id: current_user.id)
  end

  def destroy
    app = App.find(params[:id])
    app.destroy if app.user_id == current_user.id
    end
  end

  private
  def app_params
    params.permit(:image, :text)
  end

  def move_to_index
    redirect_to action: :index unless user_signed_in?
  end
end

編集画面のルーティングを設定

  • ツイートを編集するための画面に遷移したあとに、ツイートの編集を行う。
  • 編集画面への遷移にはコントローラtweetsコントローラのeditアクションを動かす。また、編集画面のパスはツイートごとに異なるものになるので、destroyメソッドの時と同じように:idのような書き方を利用する。
routes.rb
Rails.application.routes.draw do
  devise_for :users
  root  'apps#index'
  get   'apps'      =>  'apps#index'
  get   'apps/new'  =>  'apps#new'
  post  'apps'      =>  'apps#create'
  delete  'apps/:id'  => 'apps#destroy'
  get   'apps/:id/edit'  => 'apps#edit'
  get   'users/:id'  =>  'users#show'
end
  • 削除ボタンと同じように、link_toメソッドを使用する。
<div class="contents row">
  <% @apps.each do |app| %>
    <div class="content_post" style="background-image: url(<%= app.image %>);">
      <% if user_signed_in? && app.user_id == current_user.id %>
        <div class="more">
          <span><%= image_tag 'arrow_top.png' %></span>
          <ul class="more_list">
            <li>
              <%= link_to '編集', "/apps/#{app.id}/edit", method: :get %>
            </li>
            <li>
              <%= link_to '削除', "/apps/#{app.id}", method: :delete %>
            </li>
          </ul>
        </div>
      <% end %>
      <%= simple_format(app.text) %>
      <span class="name">
        <a href="/users/<%= app.user_id %>">
          <span>投稿者</span><%= app.user.nickname %>
        </a>
      </span>
    </div>
  <% end %>
  <%= paginate(@apps) %>
</div>

editアクションをコントローラに定義

class AppsController < ApplicationController

  before_action :move_to_index, except: :index

  def index
    @apps = App.includes(:user).page(params[:page]).per(5).order("created_at DESC")
  end

  def new
  end

  def create
    App.create(image: tweet_params[:image], text: tweet_params[:text], user_id: current_user.id)
  end

  def destroy
    app = App.find(params[:id])
    app.destroy if app.user_id == current_user.id
  end

  def edit
    @app = App.find(params[:id])
  end

  private
  def app_params
    params.permit(:image, :text)
  end

  def move_to_index
    redirect_to action: :index unless user_signed_in?
  end
end

編集画面の作成

<div class="contents row">
  <%= form_tag("/apps/#{@app.id}", method: :patch ) do %>
    <h3>
      編集する
    </h3>
    <input placeholder="Image Url" type="text" name="image" value="<%= @app.image %>" autofocus="true">
    <textarea cols="30" name="text" placeholder="text" rows="10"><%= @app.text %></textarea>
    <input type="submit" value="SENT">
  <% end %>
</div>
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