{"id":2811,"date":"2021-11-09T09:12:27","date_gmt":"2021-11-09T00:12:27","guid":{"rendered":"https:\/\/suzutukiblog.com\/?p=2811"},"modified":"2022-01-21T02:46:56","modified_gmt":"2022-01-20T17:46:56","slug":"rails-9","status":"publish","type":"post","link":"https:\/\/suzutukiblog.com\/index.php\/2021\/11\/09\/rails-9\/","title":{"rendered":"rails\u306e\u307e\u3068\u30815(\u30df\u30cbQ&#038;A\u30b5\u30a4\u30c8\u3092\u4f5c\u308b\u305d\u306e3)"},"content":{"rendered":"<h3><a href=\"https:\/\/suzutukiblog.com\/index.php\/2021\/04\/14\/rails-9\/\">\u305d\u306e4\u304b\u3089\u7d9a\u304d\u307e\u3059\u3002<\/a><\/h3>\n<h3>21\uff1a\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0<\/h3>\n<p><strong>app\/views\/questions\/new.html.erb<\/strong>\u3068<strong>app\/views\/questions\/edit.html.erb<\/strong><br \/>\n\u306e\u5927\u90e8\u5206\u304c\u540c\u3058\u8a18\u8ff0\u306a\u306e\u3067Dry\u539f\u5247\u3092\u7834\u3063\u3066\u3044\u308b<br \/>\n\u3067\u306f\u3069\u3046\u3059\u308b\u304b\uff1f\u5171\u901a\u5316\u3057\u307e\u3059\u3002<br \/>\n<strong>app\/questions<\/strong>\u306b\u65b0\u898f\u30d5\u30a1\u30a4\u30eb<br \/>\n<strong>_form.html.erb<\/strong>\u3092\u4f5c\u6210\u3057\u3066\u30ab\u30c3\u30c8\u30da\u30fc\u30b9\u30c8\u2193<br \/>\n\u203b\u5171\u901a\u5316\u3057\u3066\u547c\u3073\u51fa\u3059\u30d5\u30a1\u30a4\u30eb\u306b\u306f_\u3092\u3064\u3051\u308b\u306e\u304c\u30eb\u30fc\u30eb<\/p>\n<pre><strong>&lt;%= form_with model: @question, local: true do |f| %&gt;\r\n  &lt;div class=\"form-group\"&gt;\r\n    &lt;label&gt;Name&lt;\/label&gt;<\/strong>\r\n<strong>&lt;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&gt;\r\n     &lt;%= f.text_field :name, class: \"form-control\" %&gt;\r\n  &lt;\/div&gt;\r\n&lt;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&gt;\r\n  &lt;div class=\"form-group\"&gt;\r\n    &lt;label&gt;Title&lt;\/label&gt;\r\n     &lt;%= f.text_field :title, class: \"form-control\" %&gt;\r\n  &lt;\/div&gt;\r\n&lt;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&gt;\r\n  &lt;div class=\"form-group\"&gt;\r\n    &lt;label&gt;Content&lt;\/label&gt;\r\n     &lt;%= f.text_area :content, class: \"form-control\" %&gt;\r\n  &lt;\/div&gt;\r\n<\/strong>&lt;!--save\u30dc\u30bf\u30f3\u3092\u4ed8\u3051\u308b--&gt;<strong>\r\n  &lt;div class=\"text-center\"&gt;\r\n    &lt;%= f.submit \"Save\", class: \"btn btn-primary\" %&gt;\r\n  &lt;\/div&gt;\r\n&lt;% end %&gt;<\/strong><\/pre>\n<p><strong>app\/views\/questions\/new.html.erb<\/strong>\u3092\u2193\u306e\u3088\u3046\u306b\u7de8\u96c6\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>&lt;div&gt;<\/strong>\r\n<strong>  &lt;div class=\"col-md-4 offset-md-4\"&gt;<\/strong>\r\n<strong>&lt;!--\u4e2d\u592e\u306b\u5bc4\u305b\u308b\u8a2d\u5b9a--&gt;<\/strong>\r\n<strong>    &lt;h2 class=\"text-center\"&gt;New question&lt;\/h2&gt;<\/strong>\r\n<strong>   &lt;%= render 'form' %&gt;<\/strong>\r\n<strong>  &lt;\/div&gt;<\/strong>\r\n<strong> &lt;\/div&gt;<\/strong><\/pre>\n<p><strong>app\/views\/questions\/_form.html.erb\u4ee5\u4e0b\u306e\u8a18\u8ff0\u306b\u3057\u307e\u3059\u3002<\/strong><\/p>\n<pre><strong>&lt;form action=\"\/questions\" accept-charset=\"UTF-8\" method=\"post\"&gt;&lt;input name=\"utf8\" type=\"hidden\" value=\"&amp;#x2713;\" \/&gt;&lt;input type=\"hidden\" name=\"authenticity_token\" value=\"aAhSPGiLYkFNeq6FyqkvGFyLFpLooxLsNyIdR4+\/e+zq8FgqYyMlmNlWgruyu34AHg22g315u\/gbC3GMDw63bw==\" \/&gt;\r\n  &lt;div class=\"form-group\"&gt;\r\n    &lt;label&gt;Name&lt;\/label&gt;\r\n&lt;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&gt;\r\n    &lt;input class=\"form-control\" type=\"text\" name=\"question[name]\" id=\"question_name\" \/&gt;\r\n  &lt;\/div&gt;\r\n&lt;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&gt;\r\n  &lt;div class=\"form-group\"&gt;\r\n    &lt;label&gt;Title&lt;\/label&gt;\r\n     &lt;input class=\"form-control\" type=\"text\" name=\"question[title]\" id=\"question_title\" \/&gt;\r\n  &lt;\/div&gt;\r\n&lt;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&gt;\r\n  &lt;div class=\"form-group\"&gt;\r\n    &lt;label&gt;Content&lt;\/label&gt;\r\n     &lt;textarea class=\"form-control\" name=\"question[content]\" id=\"question_content\"&gt;\r\n     &lt;\/textarea&gt;\r\n  &lt;\/div&gt;\r\n&lt;!--save\u30dc\u30bf\u30f3\u3092\u4ed8\u3051\u308b--&gt;\r\n  &lt;div class=\"text-center\"&gt;\r\n    &lt;input type=\"submit\" name=\"commit\" value=\"Save\" class=\"btn btn-primary\" data-disable-with=\"Save\" \/&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/form&gt;<\/strong><\/pre>\n<p><!--more--><br \/>\n\u540c\u69d8\u306b<strong>app\/views\/questions\/edit.html.erb<\/strong>\u3082\u884c\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>&lt;div&gt;<\/strong>\r\n<strong>  &lt;div class=\"col-md-4 offset-md-4\"&gt;<\/strong>\r\n<strong>&lt;!--\u4e2d\u592e\u306b\u5bc4\u305b\u308b\u8a2d\u5b9a--&gt;<\/strong>\r\n<strong>   &lt;h2 class=\"text-center\"&gt;Edit question&lt;\/h2&gt;<\/strong>\r\n<strong>  &lt;%= render 'form' %&gt;<\/strong>\r\n<strong> &lt;\/div&gt;<\/strong>\r\n<strong>&lt;\/div&gt;<\/strong><\/pre>\n<h3>22\uff1a\u8cea\u554f\u524a\u9664\u6a5f\u80fd\u306e\u8ffd\u52a0<\/h3>\n<p>rails routes\u3067\u78ba\u8a8d\u3059\u308b\u3068\u2193<br \/>\n<strong>DELETE \/questions\/:id(.:format) questions#destroy<\/strong><\/p>\n<p><strong>app\/controllers\/questions_controller.rb<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>def destroy<\/strong>\r\n<strong>  @question = Question.find(params[:id])<\/strong>\r\n<strong>  @question.destroy<\/strong>\r\n<strong>  redirect_to root_path, notice: 'Success!'<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>\u30c7\u30b9\u30c8\u30ed\u30a4\u30a2\u30af\u30b7\u30e7\u30f3\u304c\u547c\u3070\u308c\u308b\u3068\u304d\u3001\u30af\u30a8\u30b9\u30c1\u30e7\u30f3\u306eID\u304c\u6e21\u3055\u308c\u308b\u306e\u3067\u3001\u305d\u306eID\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30fc\u30d9\u30fc\u30b9\u304b\u3089\u524a\u9664\u3057\u305f\u3044\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057@question\u306b\u4ee3\u5165\u3001<br \/>\ndestroy\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3076\u3053\u3068\u3067\u524a\u9664\u3057\u3066<br \/>\n\u30eb\u30fc\u30c8URL\u306b\u3068\u3070\u3057Success\uff01\u3092\u8868\u793a<\/p>\n<p><strong>app\/views\/questions\/index.html.erb<\/strong>\u3092<br \/>\n\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0edit\u306e\u4e0b\u306e\u90e8\u5206<\/p>\n<pre><strong>[ &lt;%= link_to 'Delete', question_path(question),<\/strong>\r\n<strong>method: :delete, data:{ confirm: 'Are you sure?'} %&gt; ]&lt;\/td&gt;<\/strong><\/pre>\n<p><strong>question_path(question)<\/strong>\u306a\u306e\u306f\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u304b\u3089<br \/>\n(question)\u306f\u5f15\u6570\u3092\u6e21\u3059<strong>(id)method: :delete,<\/strong>\u3067\u524a\u9664<br \/>\n<strong>data:{ confirm: &#8216;Are you sure?&#8217;}<\/strong>\u3067\u78ba\u8a8d\u753b\u9762\u3092\u4f5c\u308b<\/p>\n<h3>23\uff1a\u8cea\u554f\u8a73\u7d30\u753b\u9762\u3078\u306e\u30ea\u30f3\u30af<br \/>\n<!--more--><\/h3>\n<p><strong>app\/views\/questions\/index.html.erb<\/strong>\u306etitle.id\u3092\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>&lt;%= link_to question.title, question_path(question) %&gt;<\/strong>\r\n#\u2193id\u3092\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u2191question\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u3057\u3066\u3044\u308b\r\n<strong>question GET \/questions\/:id(.:format) questions#show<\/strong><\/pre>\n<p>\u8cea\u554f\u8a73\u7d30\u753b\u9762\u3078\u30ea\u30f3\u30af\u3057\u3066\u3044\u308c\u3070OK\u3067\u3059\u3002<\/p>\n<h3>24:\u56de\u7b54\u6295\u7a3f\u6a5f\u80fd\u306e\u6982\u8981<\/h3>\n<p>\u30e6\u30fc\u30b6\u30fc\u304c\u8cea\u554f\u306b\u5bfe\u3057\u3066\u3001\u56de\u7b54\u3092\u6295\u7a3f\u3067\u304d\u308b\u3002<br \/>\n\u30e6\u30fc\u30b6\u30fc\u304c\u56de\u7b54\u4e00\u89a7\u3092\u95b2\u89a7\u3067\u304d\u308b\u3002<br \/>\n\u30e6\u30fc\u30b6\u30fc\u304c\u56de\u7b54\u3092\u7de8\u96c6\u3067\u304d\u308b\u3002<br \/>\n\u30e6\u30fc\u30b6\u30fc\u304c\u56de\u7b54\u3092\u524a\u9664\u3067\u304d\u308b\u3002<\/p>\n<h3>25\uff1a\u8cea\u554f\u8a73\u7d30\u753b\u9762\u306e\u5b9f\u88c5<\/h3>\n<p><strong>app\/controllers\/questions_controller.rb<\/strong>\u2193\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>def show<\/strong>\r\n<strong>  @question = Question.find(params[:id])<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p><strong>app\/views\/questions\/show.html.erb<\/strong>\u306b\u3066\u2193\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre>#row\u306f\u30b3\u30f3\u30c6\u30ca\u3092\u6c34\u5e73\u306b\u5206\u5272\u3057\u4e0a\u304b\u3089\u4e0b\u3078\u3068\u4e26\u3079\u307e\u3059\u3002\r\n<strong>&lt;div class=\"row\"&gt;<\/strong>\r\n#row\u306e\u4e2d\u306bcol\u3092\u5b9a\u7fa9\u3057\u30ec\u30a4\u30a2\u30a6\u30c8\u3092\u7d44\u307f\u307e\u3059\r\n#\u753b\u9762\u5e45Medium\u306e\u30b0\u30ea\u30c3\u30c9\u500b\u657012\u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002\r\n  <strong>&lt;div class=\"col-md-12\"&gt;<\/strong>\r\n<strong>    &lt;h2&gt;&lt;%= @question.title %&gt;&lt;\/h2&gt;<\/strong>\r\n<strong>  &lt;\/div&gt;<\/strong>\r\n<strong>    Content: &lt;%= @question.content %&gt;<\/strong>\r\n<strong>   &lt;\/div&gt;<\/strong>\r\n<strong>  &lt;div&gt;<\/strong>\r\n<strong>    Name: &lt;%= @question.name %&gt;<\/strong>\r\n<strong>  &lt;\/div&gt;<\/strong>\r\n<strong>&lt;hr&gt;<\/strong>\r\n<strong>  &lt;div&gt;<\/strong>\r\n<strong>   &lt;%= link_to '&gt; home', root_path %&gt;<\/strong>\r\n<strong>  &lt;\/div&gt;<\/strong>\r\n<strong> &lt;\/div&gt;<\/strong>\r\n<strong>&lt;\/div&gt;<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2825\" src=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/2757e214eb85e3037cb7d16e90bd1971.png\" alt=\"\" width=\"392\" height=\"280\" srcset=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/2757e214eb85e3037cb7d16e90bd1971.png 392w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/2757e214eb85e3037cb7d16e90bd1971-300x214.png 300w\" sizes=\"auto, (max-width: 392px) 100vw, 392px\" \/><\/p>\n<p>\u8cea\u554f\u8a73\u7d30\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u3066\u3044\u308c\u3070OK\u3067\u3059\u3002<\/p>\n<h3>26\uff1aAnswers\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/h3>\n<pre><strong>rails g controller answers edit<\/strong><\/pre>\n<p>\u4fdd\u6709\u3059\u308b\u30a2\u30af\u30b7\u30e7\u30f3\u306f\u305d\u306e\u307e\u307e\u7de8\u96c6\u3059\u308b\u753b\u9762\u3067\u4f7f\u7528\u3059\u308bedit<\/p>\n<p>\u2193\u306e\u3088\u3046\u306b\u30d5\u30a1\u30a4\u30eb\u304c\u81ea\u52d5\u751f\u6210\u3055\u308c\u308b<\/p>\n<pre><strong>create app\/controllers\/answers_controller.rb<\/strong>\r\n<strong>route get 'answers\/edit'<\/strong>\r\n<strong>invoke erb<\/strong>\r\n<strong>create app\/views\/answers<\/strong>\r\n<strong>create app\/views\/answers\/edit.html.erb<\/strong>\r\n<strong>invoke test_unit<\/strong>\r\n<strong>create test\/controllers\/answers_controller_test.rb<\/strong>\r\n<strong>invoke helper<\/strong>\r\n<strong>create app\/helpers\/answers_helper.rb<\/strong>\r\n<strong>invoke test_unit<\/strong>\r\n<strong>invoke assets<\/strong>\r\n<strong>invoke coffee<\/strong>\r\n<strong>create app\/assets\/javascripts\/answers.coffee<\/strong>\r\n<strong>invoke scss<\/strong>\r\n<strong>create app\/assets\/stylesheets\/answers.scss<\/strong><\/pre>\n<p>\u91cd\u8981\u306a\u306e\u306f<strong>app\/controllers\/answers_controller.rb<\/strong><br \/>\n<strong>app\/views\/answers\/edit.html.erb<\/strong>\u3042\u3068\u306f\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306a\u3069\u3067\u3059\u3002<\/p>\n<h3>27\uff1aAnswer\u30e2\u30c7\u30eb\u306e\u4f5c\u6210<\/h3>\n<p>\u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020\u3000\u4e00\u5bfe\u591a\u306e\u95a2\u4fc2\u3067\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<p>\u3072\u3068\u3064\u306e\u8cea\u554f\u306b\u5bfe\u3057\u3066\u8907\u6570\u306e\u56de\u7b54\u3092\u8868\u793a\u3059\u308b\u305f\u3081\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u69cb\u9020\u304c\u4e00\u5bfe\u591a\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>rails g model answer question:references name:string content:text<\/strong><\/pre>\n<p>\u30e2\u30c7\u30eb\u306e\u540d\u524d\u304c\u5358\u6570\u3067\u3042\u308b\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u3001<br \/>\nquestion:references1\u5bfe\u591a\u306e\u5b9a\u7fa9\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u751f\u6210\u3055\u308c\u305f\u3082\u306e\u2193<\/p>\n<pre><strong>invoke active_record<\/strong>\r\n<strong>create db\/migrate\/20210304022906_create_answers.rb<\/strong>\r\n<strong>create app\/models\/answer.rb<\/strong>\r\n<strong>invoke test_unit<\/strong>\r\n<strong>create test\/models\/answer_test.rb<\/strong>\r\n<strong>create test\/fixtures\/answers.yml<\/strong><\/pre>\n<p><strong>db\/migrate\/20210320022906_create_answers.rb<\/strong>\u306f<\/p>\n<pre><strong>class CreateAnswers &lt; ActiveRecord::Migration[5.2]<\/strong>\r\n<strong>\r\n  def change<\/strong>\r\n<strong>    create_table :answers do |t|<\/strong>\r\n<strong>    t.references :question, foreign_key: true<\/strong>\r\n<strong>    t.string :name<\/strong>\r\n<strong>    t.text :content<\/strong>\r\n<strong>    t.timestamps<\/strong>\r\n<strong>    end<\/strong>\r\n<strong>  end<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>answers\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u305f\u3081\u306e\u5b9a\u7fa9\u304cRuby\u3067\u3055\u308c\u3066\u3044\u308b<\/p>\n<p><strong>app\/models\/answer.rb<\/strong>\u306b\u306f<\/p>\n<pre><strong>class Answer &lt; ApplicationRecord<\/strong>\r\n<strong>  belongs_to :question<\/strong>\r\n<strong>end<\/strong>\r\n#Answer\u306fquestion\u2191\u306b\u7d10\u3065\u304f(belong_to)\u3068\u3044\u3046\u610f\u5473\r\n#answer(\u7b54\u3048)\u304b\u3089\u898b\u3066question(\u8cea\u554f)\u306f\u4e00\u3064\u3042\u308b\u3068\u3044\u3046\u610f\u5473<\/pre>\n<p><strong>app\/models\/question.rb<\/strong>\u306b\u2193\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<pre>class Question &lt; ApplicationRecord\r\n\u00a0 has_many :answers, dependent: :destroy #\u2190\u8ffd\u52a0\r\n\r\n\u3000~~~~~~~~~~~~~\u4ee5\u4e0b\u7565\r\nend<\/pre>\n<p>question\u306f\u8907\u6570\u306eanswers\u3092\u6301\u3063\u3066\u3044\u308b\u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002<br \/>\n(Yahoo\u77e5\u6075\u888b\u306e\u3088\u3046\u306b\u8cea\u554f\u306b\u5bfe\u3057\u3066\u8907\u6570\u306e\u4eba\u304c\u8cea\u554f\u306b\u7b54\u3048\u3066\u304f\u308c\u308b\u304b\u3089)<\/p>\n<p>dependent: :destroy\u306f\u89aa\u3067\u3042\u308b\u8cea\u554f\u304c\u524a\u9664\u3055\u308c\u305f\u3089\u8907\u6570\u306e\u56de\u7b54\u3082<br \/>\n\u307f\u3093\u306a\u524a\u9664\u3059\u308b\u3088\u3068\u3044\u3046\u610f\u5473\u3067\u3059\u3002<\/p>\n<p>\u5909\u66f4\u3092\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u53cd\u6620\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>rails db:migrate<\/strong><\/pre>\n<p>\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u306a\u3051\u308c\u3070OK<\/p>\n<h3>28\uff1a\u56de\u7b54\u6a5f\u80fd\u95a2\u9023\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0<\/h3>\n<p><strong>app\/config\/routes.rb<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0<\/p>\n<pre><strong>root 'questions#index'<\/strong>\r\n<strong> resources :questions do<\/strong>\r\n<strong> resources :answers<\/strong>\r\n\r\n<strong>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\u4ee5\u4e0b\u7565<\/strong>\r\n<strong>end<\/strong><\/pre>\n<pre><strong>rails routes<\/strong>\r\n\u2193\u306e\u3088\u3046\u306banswers\u95a2\u9023\u306e\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u304c\u5897\u3048\u3066\u3044\u308b\u306e\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002\r\n<strong>answers#index<\/strong>\r\n<strong>POST \/questions\/:question_id\/answers(.:format) answers#create<\/strong>\r\n<strong>new_question_answer GET \/questions\/:question_id\/answers\/new(.:format) answers#new<\/strong>\r\n<strong>edit_question_answer GET \/questions\/:question_id\/answers\/:id\/edit(.:format) answers#edit<\/strong>\r\n<strong>question_answer GET \/questions\/:question_id\/answers\/:id(.:format) answers#show<\/strong>\r\n<strong>PATCH \/questions\/:question_id\/answers\/:id(.:format) answers#update<\/strong>\r\n<strong>PUT \/questions\/:question_id\/answers\/:id(.:format) answers#update<\/strong>\r\n<strong>DELETE \/questions\/:question_id\/answers\/:id(.:format) answers#destroy<\/strong><\/pre>\n<h3>29\uff1a\u56de\u7b54\u6295\u7a3f\u6a5f\u80fd<\/h3>\n<p><strong>app\/controllers\/questions_controller.rb<\/strong>\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>def show<\/strong>\r\n<strong>  @question = Question.find(params[:id])<\/strong>\r\n<strong>  @answer = Answer.new<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>\u7a7a\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u4f5c\u6210\u3057\u3066\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570@answer\u306b\u4ee3\u5165\u3057\u3066view\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<h3>30\uff1a\u56de\u7b54\u6295\u7a3f\u6a5f\u80fd\u306eview\u3092\u4f5c\u6210<\/h3>\n<p><strong>app\/views\/answers\/show.html.erb<\/strong>\u3067\u8a18\u8ff0\u3057\u307e\u3059(&lt;hr&gt;\u30bf\u30b0\u304b\u3089)<\/p>\n<pre><strong>&lt;hr&gt;<\/strong>\r\n<strong>&lt;h3&gt;Post new answer.&lt;\/h3&gt;<\/strong>\r\n<strong>  &lt;%= form_with model: [@question, @answer], local: true do |f| %&gt;<\/strong>\r\n<strong>    &lt;%= f.hidden_field :question_id, { value: @question.id} %&gt;<\/strong>\r\n<strong>      &lt;div class=\"form-group\"&gt;<\/strong>\r\n<strong>        &lt;label&gt;Name&lt;\/label&gt;<\/strong>\r\n<strong>      &lt;%=f.text_field :name, class: 'form-control' %&gt;<\/strong>\r\n<strong>      &lt;\/div&gt;<\/strong>\r\n<strong>    &lt;div class=\"form-group\"&gt;<\/strong>\r\n<strong>      &lt;label&gt;Content&lt;\/label&gt;<\/strong>\r\n<strong>        &lt;%= f.text_area :content, class: 'form-control' %&gt;<\/strong>\r\n<strong>    &lt;\/div&gt;<\/strong>\r\n<strong>   &lt;div class=\"text-center\"&gt;<\/strong>\r\n<strong>     &lt;%= f.submit \"Post\", class: 'btn btn-primary' %&gt;<\/strong>\r\n<strong>   &lt;\/div&gt;<\/strong>\r\n<strong>&lt;% end %&gt;<\/strong><\/pre>\n<p>[@question, @answer]question\u30e2\u30c7\u30eb\u306b\u7d10\u3065\u304fanswer\u30e2\u30c7\u30eb\u3092<br \/>\n\u30d5\u30a9\u30fc\u30e0\u3067\u9001\u4fe1\u3057\u305f\u3044\u6642\u306f\u3001<br \/>\nform_with\u306emodel\u306b\u914d\u5217\u3067@question\u3068@answer\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u6e21\u3057\u307e\u3059\u3002<\/p>\n<p><strong>&lt;%= f.hidden_field :question_id, { value: @question.id} %&gt;<\/strong><br \/>\n\u2191html\u306b\u306f\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u3051\u3069\u2191\u30da\u30fc\u30b8\u306b\u63cf\u753b\u3055\u308c\u306a\u3044\u5024\u3068\u3057\u3066\u30bb\u30c3\u30c8\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>2\u3064\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u5024\u306fcontroller\u306eshow\u306e\u90e8\u5206\u3067\u6e21\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>def show<\/strong>\r\n<strong>  @question = Question.find(params[:id])<\/strong>\r\n<strong>  @answer = Answer.new<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>\u3053\u306e\u3088\u3046\u306a\u30d3\u30e5\u30fc\u304c\u4f5c\u6210\u3055\u308c\u3066\u308b\u306e\u3092\u78ba\u8a8d\u3057\u305f\u3089OK\u3067\u3059\u3002<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2833\" src=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/ad143ec177038f1f768b049ea0a35651.png\" alt=\"\" width=\"1440\" height=\"602\" srcset=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/ad143ec177038f1f768b049ea0a35651.png 1440w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/ad143ec177038f1f768b049ea0a35651-300x125.png 300w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/ad143ec177038f1f768b049ea0a35651-1024x428.png 1024w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/ad143ec177038f1f768b049ea0a35651-768x321.png 768w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/04\/ad143ec177038f1f768b049ea0a35651-624x261.png 624w\" sizes=\"auto, (max-width: 1440px) 100vw, 1440px\" \/><\/strong><\/p>\n<p>\u6b21\u306bPost\u304c\u62bc\u3055\u308c\u305f\u3068\u304d\u306e\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<p><a href=\"https:\/\/suzutukiblog.com\/index.php\/2021\/04\/14\/rails-10\/\">\u305d\u306e6\u306b\u7d9a\u304d\u307e\u3059\u3002<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u305d\u306e4\u304b\u3089\u7d9a\u304d\u307e\u3059\u3002 21\uff1a\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0 app\/views\/questions\/new.html.erb\u3068app\/views\/questions\/edit.html.erb \u306e\u5927\u90e8\u5206\u304c\u540c\u3058\u8a18\u8ff0\u306a\u306e\u3067Dry\u539f\u5247\u3092\u7834\u3063 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2764,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,13],"tags":[],"class_list":["post-2811","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-note","category-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=2811"}],"version-history":[{"count":11,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2811\/revisions"}],"predecessor-version":[{"id":4492,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2811\/revisions\/4492"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/media\/2764"}],"wp:attachment":[{"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=2811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=2811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=2811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}