AR삽질러

初めてのRuby on Rails ミニQ&Aサイト開発 - 回答機能Controller, createAction, destroyAction(7) 본문

Ruby/RubyOnRails-日本語

初めてのRuby on Rails ミニQ&Aサイト開発 - 回答機能Controller, createAction, destroyAction(7)

아랑팡팡 2024. 1. 5. 14:59
728x90

 

初めてのRuby on Rails ミニQ&Aサイト開発 - 回答機能Controller, Action(7)

 

routes.rb

Rails.application.routes.draw do

  root "questions#index"
  resources :questions
  resources :questions do
    resources :answers
  end

end

root "questions#index"

 - ルートURL '/' に接続して時 QuestionsControllerのindexアクションを呼び出す。

resources :questions

 - RESTfulラウトを自動に生成する。

resources :questions do resources :answers

 - questions/:question_id/answers 形態の重複ルートを生成し、各回答が特定の質問に関連付けられていることを示す。

 

controller作成

rails g controller Answers                                       
      create  app/controllers/answers_controller.rb
      invoke  erb
      create    app/views/answers
      invoke  test_unit
      create    test/controllers/answers_controller_test.rb
      invoke  helper
      create    app/helpers/answers_helper.rb
      invoke    test_unit

 

1. create

answers_controller.rb

class AnswersController < ApplicationController
    # 回答の登録
    def create
        # p params
        @question = Question.find(params[:question_id])
        @question.answers.create(answer_params)
        redirect_to question_path(@question)
    end

    private
        def answer_params
            params.require(:answer).permit(:name, :content)
        end
end

createアクション

 - 特定の質問 ( @question )に対する新しい返事を生成する。answer_paramsメソッドを通じてフィルタリングされたパラメータを使って返事を生成する。

redirect_to question_path(@question)

 - 返事を生成し、ユーザーを該当する返事の詳細パージにリダイレクションする。

 

show.html.erb

<h1>Show</h1>
<p>
    Title : 
    <%= @question.title %><br>
</p>
<p>
    Name : 
    <%= @question.name %><br>
</p>
<p>
    Content : 
    <%= @question.content %><br>
</p>

<h2>Answers</h2>
<% @question.answers.each do |answer| %>
    <p>
        Name :<br>
        <%= answer.name %>
    </p>
    <p>
        Content :<br>
        <%= answer.content %>
    </p>
<% end %>

<h2>Add a answer</h2>
<%= form_with model: [@question, @question.answers.build] do |form| %>
    <div>
        <%= form.label :name %><br>
        <%= form.text_field :name %>
    </div>
    <div>
        <%= form.label :content %><br>
        <%= form.text_area :content %>
    </div>
    <div>
        <%= form.submit %>
    </div>
<% end %><br>

<%= link_to 'top', questions_path %>

form_with model: [@question, @quesion.answer.build]で新しい返事を生成する。このFormは AnswersControllerの createアクションにデータを送信する。

ユーザーがFormを提出したら、createアクションは新しい返事を生成し、質問の詳細パージにリダイレクションする。

 


2. destroy

 

show.html.erb

<h1>Show</h1>
<p>
    Title : 
    <%= @question.title %><br>
</p>
<p>
    Name : 
    <%= @question.name %><br>
</p>
<p>
    Content : 
    <%= @question.content %><br>
</p>

<h2>Answers</h2>
<% @question.answers.each do |answer| %>
    <p>
        Name :<br>
        <%= answer.name %>
    </p>
    <p>
        Content :<br>
        <%= answer.content %>
    </p>
    <p>
        <%= link_to 'Destroy Anser', [answer.question, answer], data: { turbo_method: 'delete', turbo_confirm: 'Ary you sure?'} %>
    </p>
<% end %>

<h2>Add a answer</h2>
<%= form_with model: [@question, @question.answers.build] do |form| %>
    <div>
        <%= form.label :name %><br>
        <%= form.text_field :name %>
    </div>
    <div>
        <%= form.label :content %><br>
        <%= form.text_area :content %>
    </div>
    <div>
        <%= form.submit %>
    </div>
<% end %><br>

<%= link_to 'top', questions_path %>

各返事に link_toヘルパーを使って、削除リンクを生成する。

リンクは AnswerControllerの destroyアクションに紐つけており、turbo_method: 'delete'オプションでHTTP DELETE要請をする。

turbo_confirm: 'Are you sure?' オプションでユーザーがリンクをクリックしたときの再確認をする。

 

answers_controller.rb

class AnswersController < ApplicationController
    # 回答の削除
    def destroy
        @question = Question.find(params[:question_id])
        @answer = @question.answers.find(params[:id])
        @answer.destroy
        redirect_to @question, status: :see_other
    end

    private
        def answer_params
            params.require(:answer).permit(:name, :content)
        end
end

destroyアクション

 - @questionに属している @answerを探して削除する。

 - @question = Question.find(params[:question_id]) は質問の中に属しているURLで idに該当する質問を探す。

 - @answer = @question.answers.find(params[:id]) は質問に属している返事の中でURLで idに該当する返事を探す。

 - @answer.destroy は該当する質問をDataBaseから削除する。

 - redirect_to @question, status: :see_other は削除した後、質問の詳細ページにリダイレクトする、この時、status: :see_otherはHTTP 状態コード303を使ってレダイレクトする

 

 


https://github.com/designAR/rails_board/tree/answer_controller_%2307

 

GitHub - designAR/rails_board

Contribute to designAR/rails_board development by creating an account on GitHub.

github.com

 

728x90
반응형
LIST