質問投稿サイトの作成(2/3)
1/3のつづき
122.質問投稿画面へのリンク
質問一覧の下に「質問入力フォーム」へのリンクを追加する
<h2>Questions</h2> <div class="row"> <div class="col-md-12"> <table class="table table-striped"> <thead class="thead-light"> <tr> <th>ID</th> <th>Title</th> <th>Menu</th> </tr> </thead> <tbody> <% @questions.each do |question| %> <tr> <td><%= question.id %></td> <td><%= question.title %></td> <td>[Edit][Delete]</td> </tr> <% end %> </tbody> </table> <!-- 追加ココから --> <div> <%= link_to 'New question', new_question_path %> </div> <!-- ココまで --> </div> </div>
123.質問編集画面へのリンク
仮のボタン「Edit」を編集画面へのリンクに変更する
<h2>Questions</h2> <div class="row"> <div class="col-md-12"> <table class="table table-striped"> <thead class="thead-light"> <tr> <th>ID</th> <th>Title</th> <th>Menu</th> </tr> </thead> <tbody> <% @questions.each do |question| %> <tr> <td><%= question.id %></td> <td><%= question.title %></td> <td>[<%= link_to 'Edit', edit_question_path(question) %>] [Delete]</td> <!-- 変更 --> </tr> <% end %> </tbody> </table> <div> <%= link_to 'New question', new_question_path %> </div> </div> </div>
edit_question GET /questions/:id/edit(.:format) questions#edit
これでidが編集画面にわたり利用することができる.
リンク[Edit]にカーソルをあてて右クリックで「検証」.
すると生成されているHTMLが見れる.
124.質問編集画面View
new.html.erb
と基本同じなので,コピペする
<div> <div class="col-md-4 offset-md-4"> <h2 class="text-center">Edit question</h2> <!-- 違うのはここだけ --> <%= form_with model: @question, local:true do |f| %> <div class="form-group"> <label>Name</label> <%= f.text_field :name, class: "form-control" %> </div> <div class="form-group"> <label>Title</label> <%= f.text_field :title, class: "form-control" %> </div> <div class="form-group"> <label>Content</label> <%= f.text_area :content, class: "form-control" %> </div> <div class="text-center"> <%= f.submit "Save", class: "btn btn-primary" %> </div> <% end %> </div> </div>
編集欄に何も表示されていないことに注意する.
これはインスタンス変数@questionが空のため,Railsが新規入力と判断したためなのか,単に表示するものがないからのどちらかだろう.次では,editアクションで@questionにデータをわたす.
125.質問編集画面Controller
# すべて追加 def edit @question = Question.find(params[:id]) end def update @question = Question.find(params[:id]) if @question.update(question_params) redirect_to root_path, notice: 'Success!' else flash[:alert]='Save error!' render :edit end end
今度はちゃんと編集欄に表示さている
編集し、保存してみる
編集できた!
127.Viewのリファクタリング
new.html.erb
とedit.html.erb
のviewはほとんど同じなので,viewを共通化する。
共通しているのは↓この部分.
<%= form_with model: @question, local:true do |f| %> <div class="form-group"> <label>Name</label> <%= f.text_field :name, class: "form-control" %> </div> <div class="form-group"> <label>Title</label> <%= f.text_field :title, class: "form-control" %> </div> <div class="form-group"> <label>Content</label> <%= f.text_area :content, class: "form-control" %> </div> <div class="text-center"> <%= f.submit "Save", class: "btn btn-primary" %> </div> <% end %>
<div> <div class="col-md-4 offset-md-4"> <h2 class="text-center">New question</h2> <%= render 'form' %> </div> </div>
<div> <div class="col-md-4 offset-md-4"> <h2 class="text-center">Edit question</h2> <%= render 'form' %> </div> </div>
128.質問削除機能の追加
投稿した質問を削除できるようにする.まずはコントローラ側から
def destroy @question = Question.find(params[:id]) @question.destroy redirect_to root_path, notice: 'Success!' end
次はview側
仮のボタン[Delete]をリンクに変更
<h2>Questions</h2> <div class="row"> <div class="col-md-12"> <table class="table table-striped"> <thead class="thead-light"> <tr> <th>ID</th> <th>Title</th> <th>Menu</th> </tr> </thead> <tbody> <% @questions.each do |question| %> <tr> <td><%= question.id %></td> <td><%= question.title %></td> <td>[<%= link_to 'Edit', edit_question_path(question) %>] [<%= link_to 'Delete', question_path(question), method: :delete, data: {confirm: 'R U sure?'} %>]</td> </tr> <!-- ↑変更 --> <% end %> </tbody> </table> <div> <%= link_to 'New question', new_question_path %> </div> </div> </div>
4番目の投稿を削除してみる
削除できた!
129.質問詳細画面へのリンク
投稿タイトルをリンクに変更し,詳細画面に遷移できるようにする
<h2>Questions</h2> <div class="row"> <div class="col-md-12"> <table class="table table-striped"> <thead class="thead-light"> <tr> <th>ID</th> <th>Title</th> <th>Menu</th> </tr> </thead> <tbody> <% @questions.each do |question| %> <tr> <td><%= question.id %></td> <td><%= link_to question.title, question_path(question) %></td> <!-- 変更 --> <td>[<%= link_to 'Edit', edit_question_path(question) %>] [<%= link_to 'Delete', question_path(question), method: :delete, data: {confirm: 'R U sure?'} %>]</td> </tr> <% end %> </tbody> </table> <div> <%= link_to 'New question', new_question_path %> </div> </div> </div>
タイトルがリンクに変更
どのリンクをクリックしてもshowアクションとviewを実装していないため,同じviewになる
rails routesでshowメソッドのルーティングを確認
question GET /questions/:id(.:format) questions#show
viewの実装はこの後