LoginSignup
1
1

More than 3 years have passed since last update.

つぶやきアプリを作ってみよう⑦

Posted at

次はつぶやき詳細ページを作っていきましょう。

showアクションのルーティングを設定します。

config/routes.rb
Rails.application.routes.draw do
  root to: 'tweets#index'
  resources :tweets, only: [:index, :new, :create, :destroy, :edit, :update, :show]
end

7つのアクションが揃ったのでonlyオプションは必要なくなるのでまとめます。

config/routes.rb
Rails.application.routes.draw do
  root to: 'tweets#index'
  resources :tweets

image.png

ルーティングが設定されました。

トップページのビューを編集しましょう。

app/views/tweets/index.html.erb
<div class="contents row">
  <% @tweets.each do |tweet| %>
    <div class="content_post" >
      <p><%= tweet.text %></p>
       <p><%= image_tag tweet.image.variant(resize: '500x500'), class: 'tweet-image' if tweet.image.attached?%></p>
      <span class="name">
        <%= tweet.name %>
      </span>
      <%= link_to '詳細', tweet_path(tweet.id), method: :get %>
      <%= link_to '編集', edit_tweet_path(tweet.id), method: :get %>
      <%= link_to '削除', "/tweets/#{tweet.id}", method: :delete %>
    </div>
  <% end %>
</div>

コントローラーにshowアクションを追記しましょう。

app/controllers/tweets_controller.rb
class TweetsController < ApplicationController

  def index
    @tweets = Tweet.all
  end

  def new
    @tweet = Tweet.new
  end

  def create
    Tweet.create(tweet_params)
  end

  def destroy
    tweet = Tweet.find(params[:id])
    tweet.destroy
  end

  def edit
    @tweet = Tweet.find(params[:id])
  end

  def update
    tweet = Tweet.find(params[:id])
    tweet.update(tweet_params)
  end

  def show
    @tweet = Tweet.find(params[:id])
  end

  private
  def tweet_params
    params.require(:tweet).permit(:name,:text,:image)
  end
end

詳細ページのビューを設定します。

app/views/tweets/show.html.erb
<div class="contents row">
    <div class="content_post" >
      <p><%= tweet.text %></p>
       <p><%= image_tag tweet.image.variant(resize: '500x500'), class: 'tweet-image' if tweet.image.attached?%></p>
      <span class="name">
        <%= tweet.name %>
      </span>
      <%= link_to '編集', edit_tweet_path(tweet.id), method: :get %>
      <%= link_to '削除', "/tweets/#{tweet.id}", method: :delete %>
    </div>
</div>

以下の挙動になっていれば成功です。

neko5.gif

以上で機能としては完成です。

しかしコントローラーの記述を見ると同じ記述が繰り返し使われています。
同じ処理を一つにまとめて、リファクタリングしましょう。

before_action

コントローラで定義されたアクションが実行される前に、共通の処理を行うことができます。

class コントローラ名 < ApplicationController
  before_action :処理させたいメソッド名

editアクションとshowアクションの記述が同じなので
set_tweetというアクションにまとめてbefore_actionに設定しましょう。

app/controllers/tweets_controller.rb
class TweetsController < ApplicationController
  before_action :set_tweet, only: [:edit, :show]

  def index
    @tweets = Tweet.all
  end

  def new
    @tweet = Tweet.new
  end

  def create
    Tweet.create(tweet_params)
  end

  def destroy
    tweet = Tweet.find(params[:id])
    tweet.destroy
  end

  def edit
  end

  def update
    tweet = Tweet.find(params[:id])
    tweet.update(tweet_params)
  end

  def show
  end

  private

  def tweet_params
    params.require(:tweet).permit(:name, :image, :text)
  end

  def set_tweet
    @tweet = Tweet.find(params[:id])
  end
end
1
1
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
1