Yuki Matsumoto

To be engneer soon / Ruby on Rails

ルーティングの復習とupdateアクションの追加

2019-02-19 Yuki MatsumotoRuby on Rails

自分の理解度の整理も踏まえて、GETメソッドやURIメソッド、コントローラーの関係性について書いてみます。

まず、処理の実行においてはそれぞれのメソッドが呼ばれることでURIへのリクエストが送られます。そしてリクエストを受けたらそれぞれのコントローラーアクションが呼ばれます。

以下のルーティングを見てみましょう。

Prefix Verb   URI Pattern                                                                              Controller#Action
                     root GET    /                                                                                        ideas#index
                    ideas GET    /ideas(.:format)                                                                         ideas#index
                          POST   /ideas(.:format)                                                                         ideas#create
                 new_idea GET    /ideas/new(.:format)                                                                     ideas#new
                edit_idea GET    /ideas/:id/edit(.:format)                                                                ideas#edit
                     idea GET    /ideas/:id(.:format)                                                                     ideas#show
                          PATCH  /ideas/:id(.:format)                                                                     ideas#update
                          PUT    /ideas/:id(.:format)                                                                     ideas#update
                          DELETE /ideas/:id(.:format)                                                                     ideas#destroy

例えば、GETメソッドが呼ばれると/ideasがリクエストされ、そのリクエストに対してindexアクションが呼ばれます。

updateアクションを追加する

それではupdateアクションを追加していきます。ideas_controller.rbに記述します。

  def show
  end

  def new
    @idea = Idea.new
  end

  def create
    @idea = Idea.new(idea_params)
    if @idea.save
      redirect_to ideas_path, notice: 'Success!'
    else
      flash[:alert] = 'Save error!'
      render :new
    end
  end

  def edit
    @idea = Idea.find(params[:id])
  end

  def update
    #updateアクションを追加
  end

  private
    def idea_params
      params.require(:idea).permit(:title, :content)
    end

end

編集画面で登録ボタンが押されると、入力した情報がサーバーへ送られ、updateアクションがその情報を受け取り、データベースを書き換えます。

updateアクションは特定のIDを取ってくる必要があるので以下のように書きます。

  def update
    @idea = Idea.find(params[:id])
    redirect_to ideas_path
  end

重複しているクラスを整理する

次にeditアクションとupdateアクションのfindメソッドが重複しているので、privateメソッドにfind_idea_by_idを定義します。

  def edit
    @idea = find_idea_by_id
  end

  def update
    @idea = find_idea_by_id
    @idea.update(idea_params)
    redirect_to ideas_path
  end

  private
    def idea_params
      params.require(:idea).permit(:title, :content)
    end

  def find_idea_by_id
    Idea.find(params[:id])
  end

これで可読性と保守性があるコードにすることができます。

編集画面へリンクをつける

一覧画面から編集画面へのリンクを作成します。編集するファイル名はindex.html.erb

<div align="center">
<h2>IdeaBrite</h2>
    <table>
        <% @ideas.each do |idea| %>
            <tr>
                <th><%= idea.title %></th>
            </tr>
            <tr>
                <th><%= idea.content %></th>
            </tr>
                <th><%= link_to 'Edit', edit_idea_path(idea.id) %></th>
        <% end %>
    </table>
    <%= link_to "Post", new_idea_path %>
</div>

htmlを大きく書き換えていますが、<%= link_to 'Edit', edit_idea_path(idea.id) %>をつける以外はそのままです。これをつけることで各投稿内容の編集画面へ移動することが可能です。

まとめ

今日はルーティングの関係性についてのおさらいとUpdateアクションの追加を行いました。明日は削除機能の実装を行っていこうと思います。ここまでできれば週末はデザインにがっつり時間を使えそうです。