일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- Python
- C로 시작하는 컴퓨터 프로그래밍4판
- 単語
- java
- CSS
- Flutter
- nico
- 인프런
- 건담
- 디지몬
- Web
- 반다이몰
- jsp
- 자바
- 일본어
- DART
- 건담베이스
- 一日一つメソッド
- rails
- javascript
- html
- Spring
- vscode
- メソッド
- springboot
- 日本語
- 비즈니스일본어
- rails7
- ruby
- 연습문제
- Today
- Total
AR삽질러
初めてのRuby on Rails ミニQ&Aサイト開発 - 改善、回答機能(6) 본문
初めてのRuby on Rails ミニQ&Aサイト開発 - 改善、回答機能(6)
- 質問一覧パージに、新規質問作成画面へのリンクを設定
- 質問詳細画面から、一覧ページへ戻るリンクを設定
- rootのルーチィングを設定
1. 改善
routes.rb
Rails.application.routes.draw do
root "questions#index"
resources :questions
end
root "questions#index"
- アプリケーションのルートURL ( '/' )に接近した時、QuestionsControllerのindexアクションを呼び出す。
resources :questions
- questionsリソーずのRESTfulラウトを自動に生成し、index, show, new, create, edit, update, destroy CRUDアクションのラウトを作る。
index.html.erb
<h1>Questions</h1>
<%= link_to 'new', new_question_path %>
<table>
<tr>
<th>ID</th>
<th>Title</th>
<th>Name</th>
<th>Content</th>
</tr>
<% @questions.each do |question| %>
<tr>
<td><%= question.id %></td>
<td><%= question.title %></td>
<td><%= question.name %></td>
<td><%= question.content %></td>
<td><%= link_to 'show', question_path(question) %></td>
<td><%= link_to 'edit', edit_question_path(question) %></td>
<td><%= link_to 'destroy', question_path(question),
data: { turbo_method: 'delete', turbo_confirm: 'Are you sure?' }%></td>
</tr>
<% end %>
</table>
登録されている全ての質問( @questions )をテーブル形式で表紙する。
各質問は id, title, name, content フィールドを表紙し、show, edit, destroyのlinkを含む。
destroy link
- link_to へルーパを使って質問を削除するlinkを作る。turbo_method: 'delete' オプションを使ってHTTP DELETE要請し、ユーザーがlinkをクリックしたら 'turbo_confim' オプションに設定されている確認メッセージが表紙される。
show.html.erb
<h1>Show</h1>
<p>
Title :
<%= @question.title %><br>
</p>
<p>
Name :
<%= @question.name %><br>
</p>
<p>
Content :
<%= @question.content %><br>
</p>
<%= link_to 'top', questions_path %>
削除機能( destroy )
- link_toヘルパーを使って削除ブタンを実装する。
- turbo_method: delete オプションを使って、linkをクリックした時、HTTP DELETEを要請をサーバに送って質問を削除する。
- turbo_confirm: 'Are you sure?' オプションはユーザーがリンクをクリックした時に確認メッセージを表紙する。
- サーバでは 'QuestionsController'の 'destroy'アクションを通じてリクエストを処理し、質問を削除する。削除ができたら、indexpageに戻る。
2. 回答機能
model作成
rails g model Answer name:string content:text question:references
invoke active_record
create db/migrate/20240105033818_create_answers.rb
create app/models/answer.rb
invoke test_unit
create test/models/answer_test.rb
create test/fixtures/answers.yml
rails db:migrate
== 20240105033818 CreateAnswers: migrating ====================================
-- create_table(:answers)
-> 0.0010s
== 20240105033818 CreateAnswers: migrated (0.0010s) ===========================
name
- 返事の名前を表す文字列。
content
- 返事の内容を保存するtextフィールド。
question_id
- QuestionモデルのForeignKeyとして、該当する返事がどんな質問に属しているのかを表す。
model/answer.rb
class Answer < ApplicationRecord
belongs_to :question
end
delongs_to :question
- AnswerモデルがQuesionモデルに属していることを示す。
各Answerインスタンスは一つのQuestionインスタンスと連関され、Answerオブジェクトは .questionメソッドを通じて質問に接近でくるようになる。
migration
class CreateAnswers < ActiveRecord::Migration[7.1]
def change
create_table :answers do |t|
t.string :name
t.text :content
t.references :question, null: false, foreign_key: true
t.timestamps
end
end
end
answersテーブルを生成する | |
id | 基本キーとして自動に生成される |
name | 文字列の、登録者の名前を保存する |
content | textで、返事の内容を保存する |
question_id | 定数、関連された質問のidを参照するForeignKeyで、null: falseオプションが設定され、必ず値がなければならない。 |
create_at, update_at | timestampsで、返事が生成されたり、更新された時間を自動に保存する |
routes.rb
Rails.application.routes.draw do
root "questions#index"
resources :questions
resources :question do
resources :answers
end
end
- resources :questions do ブロック内部に resources :answersを書いて重ね合わせルートを作成する。 これにより、answersコントローラの標準CRUDアクションにアクセスする際に、question_idパラメータを使用することができる。
- answersコントローラを実装すると、このコントローラは回答を生成、照会、修正、削除するロジックを担当し、 各アクションは、関連する質問のコンテキスト内で実行さレル。
https://github.com/designAR/rails_board/tree/answer_model_%2306
'Ruby > RubyOnRails-日本語' 카테고리의 다른 글
Rails 一日一つメソッド (Day16) resourcesメソッド (0) | 2024.01.05 |
---|---|
初めてのRuby on Rails ミニQ&Aサイト開発 - 回答機能Controller, createAction, destroyAction(7) (2) | 2024.01.05 |
Rails 一日一つメソッド (Day15) form_withメソッド (0) | 2024.01.04 |
初めてのRuby on Rails ミニQ&Aサイト開発 - delete, destroy 削除(5) (0) | 2024.01.04 |
初めてのRuby on Rails ミニQ&Aサイト開発 - edit, update 更新(4) (0) | 2024.01.04 |