{"id":2827,"date":"2021-11-10T12:13:39","date_gmt":"2021-11-10T03:13:39","guid":{"rendered":"https:\/\/suzutukiblog.com\/?p=2827"},"modified":"2022-01-15T02:37:06","modified_gmt":"2022-01-14T17:37:06","slug":"rails-10","status":"publish","type":"post","link":"https:\/\/suzutukiblog.com\/index.php\/2021\/11\/10\/rails-10\/","title":{"rendered":"rails\u306e\u307e\u3068\u30816(\u30df\u30cbQ&#038;A\u30b5\u30a4\u30c8\u3092\u4f5c\u308b\u305d\u306e4)"},"content":{"rendered":"<p><a href=\"https:\/\/suzutukiblog.com\/index.php\/2021\/04\/14\/rails-9\/\">\uff15\u304b\u3089\u7d9a\u304d\u307e\u3059\u3002<\/a><\/p>\n<h3>31\uff1aPost\u3092\u62bc\u3057\u305f\u6642\u306e\u4fdd\u5b58\u51e6\u7406\u306e\u958b\u767a<\/h3>\n<pre>rails routes #\u4fdd\u5b58\u3059\u308b\u969b\u306b\u5fc5\u8981\u306a\u60c5\u5831\u3092\u898b\u308b\r\n<strong>POST \/questions\/:question_id\/answers(.:format) answers#create<\/strong><\/pre>\n<p><strong>app\/controllers\/answers_controller.rb<\/strong><\/p>\n<pre>\u00d7\u3060\u3081\u306a\u4f8b\r\n<strong>class AnswersController &lt; ApplicationController<\/strong>\r\n\r\n<strong>  def create<\/strong>\r\n<strong>    @question = Question.find(params[:question_id])<\/strong>\r\n<strong>    @answer = Answer.new<\/strong>\r\n<strong>     if @answer.save<\/strong>\r\n<strong>       redirect_to root_path, notice: 'Success!'<\/strong>\r\n<strong>     else<\/strong>\r\n<strong>       flash[:alert] = 'Save error!'<\/strong>\r\n<strong>     render :show<\/strong>\r\n<strong>   end<\/strong>\r\n<strong>  end<\/strong>\r\n\r\n<strong>   def edit<\/strong>\r\n<strong>   end<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>view\u306b\u5024\u3092\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308b\u5909\u6570\u306e\u5834\u5408\u306f\u3001@\u3092\u4ed8\u3051\u308b\u3002<br \/>\n\u9006\u306b\u3001view\u306b\u5024\u3092\u6e21\u3059\u5fc5\u8981\u304c\u3042\u308b\u5909\u6570\u306e\u5834\u5408\u306f@\u3092\u4ed8\u3051\u306a\u3044\uff08\u4ed8\u3051\u3066\u3082\u52d5\u4f5c\u3059\u308b\uff09\u3002<br \/>\n\u72b6\u6cc1\u306b\u5fdc\u3058\u3066\u3001\u4f7f\u3044\u5206\u3051\u3092\u3057\u3066\u3044\u308b\u305f\u3081\u3001<br \/>\n\u3084\u308a\u305f\u3044\u51e6\u7406\u306b\u3088\u3063\u3066\u4ed8\u3051\u305f\u308a\u4ed8\u3051\u306a\u304b\u3063\u305f\u308a\u3059\u308b\u3002<br \/>\n\u25ef\u826f\u3044\u4f8b<\/p>\n<pre><strong>class AnswersController &lt; ApplicationController<\/strong>\r\n\r\n<strong>def create<\/strong>\r\n<strong>  @question = Question.find(params[:question_id])<\/strong>\r\n<strong>  @answer = Answer.new<\/strong>\r\n<strong>    if @answer.update(answer_params)<\/strong>\r\n<strong>      redirect_to question_path(@question), notice: 'Success!'<\/strong>\r\n<strong>    else<\/strong>\r\n<strong>      redirect_to question_path(@question), notice: 'Invalid!'<\/strong>\r\n<strong>  end<\/strong>\r\n<strong>end<\/strong>\r\n\r\n<strong>  def edit<\/strong>\r\n<strong>  end<\/strong>\r\n\r\n<strong>private<\/strong>\r\n<strong>  def answer_params<\/strong>\r\n<strong>    params.require(:answer).permit(:content, :name, :question_id)<\/strong>\r\n<strong>  end<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>\u30b9\u30c8\u30ed\u30f3\u30b0\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u2191\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>32:Answer\u30e2\u30c7\u30eb\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3<\/h3>\n<p>\u30d5\u30a9\u30fc\u30e0\u306b\u7a7a\u3067\u4fdd\u5b58\u3059\u308b\u306e\u3092\u7981\u6b62\u3057\u307e\u3059\u3002<br \/>\n<strong>app\/models\/answer.rb<\/strong>\u306b\u8a18\u8ff0\u2193<\/p>\n<pre><strong>class Answer &lt; ApplicationRecord<\/strong>\r\n<strong>  belongs_to :question<\/strong>\r\n<strong>    validates :content, presence: true<\/strong>\r\n<strong>    validates :name, presence: true<\/strong>\r\n<strong>end<\/strong><\/pre>\n<h3>33\uff1a\u56de\u7b54\u4e00\u89a7\u8868\u793a<\/h3>\n<pre><strong>app\/views\/answers\/show.html.erb\u3067<\/strong>\u8a18\u8ff0\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\r\n<strong>&lt;hr&gt;<\/strong>\r\n\r\n<strong>&lt;div&gt;<\/strong>\r\n<strong>&lt;h3&gt;Answers&lt;\/h3&gt;<\/strong>\r\n<strong>  &lt;table class=\"table table-striped\"&gt;<\/strong>\r\n<strong>    &lt;% if @question.answers.any? %&gt;<\/strong>\r\n<strong>      &lt;thead class=\"thead-light\"&gt;<\/strong>\r\n<strong>        &lt;tr&gt;<\/strong>\r\n<strong>          &lt;td&gt;Answer&lt;\/td&gt;<\/strong>\r\n<strong>          &lt;td&gt;Name&lt;\/td&gt;<\/strong>\r\n<strong>           &lt;td&gt;Menu&lt;\/td&gt;<\/strong>\r\n<strong>        &lt;\/tr&gt;<\/strong>\r\n<strong>      &lt;\/thead&gt;<\/strong>\r\n<strong>   &lt;tbody&gt;<\/strong>\r\n<strong>     &lt;% @question.answers.each do |answer| %&gt;<\/strong>\r\n<strong>      &lt;tr&gt;<\/strong>\r\n<strong>       &lt;td&gt;&lt;%= answer.content %&gt;&lt;\/td&gt;<\/strong>\r\n<strong>       &lt;td&gt;&lt;%= answer.name %&gt;&lt;\/td&gt;<\/strong>\r\n<strong>       &lt;td&gt;[edit] [Delete]&lt;\/td&gt;<\/strong>\r\n<strong>      &lt;\/tr&gt;<\/strong>\r\n<strong>    &lt;% end %&gt;<\/strong>\r\n<strong>   &lt;\/tbody&gt;<\/strong>\r\n<strong>  &lt;% else %&gt;<\/strong>\r\n<strong>   &lt;p&gt;No answer yet.&lt;\/p&gt;<\/strong>\r\n<strong> &lt;% end %&gt;<\/strong>\r\n<strong>&lt;\/table&gt;<\/strong>\r\n<strong>&lt;\/div&gt;<\/strong><\/pre>\n<pre><strong>@question = Question.find<\/strong>\r\n#Quesutions\u30c6\u30fc\u30d6\u30eb\u304b\u3089find\u3057\u304b\u3057\u3066\u3044\u306a\u3044\u304c\r\n\r\n<strong>class Question &lt; ApplicationRecord<\/strong>\r\n<strong>  has_many :answers<\/strong><\/pre>\n<p>\u56de\u7b54\u4e00\u89a7\u304c\u8868\u793a\u3055\u308c\u305f\u3089OK\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2838\" src=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/404b8df0d73f61bd21c67ae715a4be8f.png\" alt=\"\" width=\"1540\" height=\"820\" srcset=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/404b8df0d73f61bd21c67ae715a4be8f.png 1540w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/404b8df0d73f61bd21c67ae715a4be8f-300x160.png 300w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/404b8df0d73f61bd21c67ae715a4be8f-1024x545.png 1024w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/404b8df0d73f61bd21c67ae715a4be8f-768x409.png 768w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/404b8df0d73f61bd21c67ae715a4be8f-1536x818.png 1536w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/404b8df0d73f61bd21c67ae715a4be8f-624x332.png 624w\" sizes=\"auto, (max-width: 1540px) 100vw, 1540px\" \/><\/p>\n<p><!--more--><br \/>\n\u3053\u308c\u306b\u3088\u3063\u3066\u4e00\u3064\u306e\u8cea\u554f\u306b\u591a\u304f\u306e\u56de\u7b54\u304c\u7d10\u4ed8\u3044\u3066\u3044\u3066<br \/>\nQuestion\u30e2\u30c7\u30eb\u3092find\u3059\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u81ea\u52d5\u7684\u306b\u7d10\u3065\u304f<br \/>\nanswers\u306e\u30c7\u30fc\u30bf\u3082\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n\u305d\u3057\u3066<strong>&lt;% @question.answers.each do |answer| %&gt;<\/strong>\u306e\u90e8\u5206\u3067<br \/>\n@question\u306b\u7d10\u3065\u304fanswers\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u308b<br \/>\nanswers\u306b\u306f\u914d\u5217\u3067\u30c7\u30fc\u30bf\u304c\u5165\u3063\u3066\u3044\u308b\u304b\u3089|answer|<\/p>\n<p>SQL\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u2193\u767a\u884c\u3055\u308c\u305fSQL\u306f\u9069\u5b9c\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\uff01<\/p>\n<pre><strong>SELECT \"questions\".* FROM \"questions\" WHERE \"questions\".\"id\" = ? LIMIT ? [[\"id\", 10], [\"LIMIT\", 1]]<\/strong>\r\n<strong>\u21b3 app\/controllers\/questions_controller.rb:7<\/strong>\r\n<strong>Rendering questions\/show.html.erb within layouts\/application<\/strong>\r\n<strong>Answer Exists (0.1ms) SELECT 1 AS one FROM \"answers\" WHERE \"answers\".\"question_id\" = ? LIMIT ?<\/strong><\/pre>\n<h3>34\uff1a\u56de\u7b54\u306e\u7de8\u96c6<\/h3>\n<p>\u56de\u7b54\u306eedit\u3092\u4f5c\u308a\u307e\u3059\u3002rails routes\u3067\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p><strong>edit_question_answer GET \/questions\/:question_id\/answers\/:id\/edit(.:format) answers#edit<\/strong><\/p>\n<p>\u307e\u305a\u30ea\u30f3\u30af\u3092\u4f5c\u30ea\u307e\u3059\u3002<br \/>\n<strong>app\/views\/answers\/show.html.erb<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<p><strong>&lt;%= link_to &#8216;edit&#8217;, edit_question_answer_path(answer) %&gt;<\/strong><\/p>\n<p><strong>app\/controllers\/answers_controller.rb<\/strong>\u306b\u3066\u8a18\u8ff0<\/p>\n<pre><strong>def edit<\/strong>\r\n<strong>  @question = Question.find(params[:question_id])<\/strong>\r\n<strong>  @answer = @question.answers.find(params[:id])<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>@question\u306e\u8a72\u5f53\u306eanswers_id\u3092\u53d6\u5f97\u3057\u3066@answer\u306b\u4ee3\u5165<br \/>\n\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5909\u6570\u306a\u306e\u3067view\u306b\u308f\u305f\u3059\u3053\u3068\u304c\u3067\u304d\u308b<strong>\u3002<\/strong><\/p>\n<p>\u6b21\u306bview\u306e\u7de8\u96c6\u3092\u3059\u308b<br \/>\n<strong>app\/views\/questions\/show.html.erb<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059<strong>\u3002<\/strong><br \/>\n\u7b2c\u4e00\u5f15\u6570\u306b@question\u7b2c2\u5f15\u6570\u306banswer\u306b\u3057\u307e\u3059\u3002<br \/>\n<strong>&lt;%= link_to &#8216;edit&#8217;, edit_question_answer_path(@question, answer) %&gt;<\/strong><\/p>\n<pre><strong>app\/views\/answers\/edit.html.erb<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002\r\n\r\n<strong>&lt;div&gt;<\/strong>\r\n<strong>&lt;h2&gt;Update answer&lt;\/h2&gt;<\/strong>\r\n<strong>  &lt;%= form_with model:[@question, @answer], local: true do |f| %&gt;<\/strong>\r\n<strong>    &lt;div class=\"form-group\"&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;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&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;!--\u30c6\u30ad\u30b9\u30c8\u30d5\u30a3\u30fc\u30eb\u30c9\u3092\u3064\u3051\u308b--&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_field :content, class: \"form-control\" %&gt;<\/strong>\r\n<strong>  &lt;\/div&gt;<\/strong>\r\n&lt;!--save\u30dc\u30bf\u30f3\u3092\u4ed8\u3051\u308b--&gt;\r\n<strong>  &lt;div class=\"text-center\"&gt;<\/strong>\r\n<strong>    &lt;%= f.submit \"Update\", class: \"btn btn-primary\" %&gt;<\/strong>\r\n<strong>  &lt;\/div&gt;<\/strong>\r\n<strong> &lt;\/div&gt;<\/strong>\r\n<strong>&lt;% end %&gt;<\/strong>\r\n<strong>&lt;\/div&gt;<\/strong><\/pre>\n<p>\u66f4\u65b0\u753b\u9762\u304c\u8868\u793a\u3055\u308c\u305f\u3089OK\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2839\" src=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/75e4a91a59e42baf4c0d9c1a15280a3e.png\" alt=\"\" width=\"1436\" height=\"804\" srcset=\"https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/75e4a91a59e42baf4c0d9c1a15280a3e.png 1436w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/75e4a91a59e42baf4c0d9c1a15280a3e-300x168.png 300w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/75e4a91a59e42baf4c0d9c1a15280a3e-1024x573.png 1024w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/75e4a91a59e42baf4c0d9c1a15280a3e-768x430.png 768w, https:\/\/suzutukiblog.com\/wp-content\/uploads\/2021\/01\/75e4a91a59e42baf4c0d9c1a15280a3e-624x349.png 624w\" sizes=\"auto, (max-width: 1436px) 100vw, 1436px\" \/><\/p>\n<p>\u7d9a\u304d\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u78ba\u8a8d\u2193<\/p>\n<p><strong>PATCH \/questions\/:id(.:format) questions#update<\/strong><br \/>\n<strong>PUT \/questions\/:id(.:format) questions#update<\/strong><\/p>\n<p>PUT\u3084PATCH:\u30ea\u30bd\u30fc\u30b9\u306e\u4f5c\u6210\u307e\u305f\u306f\u7f6e\u63db\u306e\u6642\u306b\u4f7f\u308f\u308c\u308bhtml\u30e1\u30bd\u30c3\u30c9POST\u3068\u4f3c\u3066\u3044\u308b\u304c\u53b3\u5bc6\u306b\u306f\u9055\u3046\u3082\u306e\u3067\u3059\u3002<\/p>\n<p><strong>app\/controllers\/answers_controller.rb<\/strong>\u306b\u3066private\u306e\u4e0a\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>def update<\/strong>\r\n<strong>  @question = Question.find(params[:question_id])<\/strong>\r\n<strong>  @answer = @question.answers.find(params[:id])<\/strong>\r\n<strong>    if @answer.update(answer_params)<\/strong>\r\n<strong>      redirect_to question_path(@question), notice: 'Success!'<\/strong>\r\n<strong>    else<\/strong>\r\n<strong>     flash[:alert] = 'Invalid!'<\/strong>\r\n<strong>     render :edit<\/strong>\r\n<strong> end<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>redirect_to question_path(@question)\u2190\u306e\u90e8\u5206\u306fid\u306e\u90e8\u5206\u306a\u306e\u3067\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b<\/p>\n<p>&nbsp;<\/p>\n<h3>35\uff1a\u56de\u7b54\u306e\u524a\u9664\u306e\u5b9f\u88c5<\/h3>\n<p>\u307e\u305a\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306e\u78ba\u8a8d\u3092\u3057\u307e\u3059\u3002<\/p>\n<p><strong>rails routes<\/strong><\/p>\n<pre><strong>question_answer GET<\/strong>\r\n<strong>\/questions\/:question_id\/answers\/:id(.:format) answers#show<\/strong><\/pre>\n<p><strong>app\/views\/questions\/show.html.erb<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>&lt;%= link_to 'Delete', question_answer_path(@question, answers),<\/strong><strong>method :delete, data:{ confirm: 'Are you sure?' } %&gt;<\/strong><\/pre>\n<p>\u4e8b\u4f8b\u2460 question_id \u306e\u307f\u304c\u5fc5\u8981\u306a\u5834\u5408<br \/>\n<strong>\/questions\/:question_id\/answers<\/strong><br \/>\n\u2192\u3000URI\u306b\u6e21\u3059\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306f\u3001:question_id \u306e1\u3064\u3002<\/p>\n<p>\u4e8b\u4f8b\u2461 question\u3068answer \u304c\u5fc5\u8981\u306a\u5834\u5408<br \/>\n<strong>\/questions\/:question_id\/answers\/:id<\/strong><br \/>\n\u2192\u3000URI\u306b\u6e21\u3059\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306f\u3001:question_id \u3001(answer\u306e):id\u306e\u30012\u3064\u3068\u306a\u308b\u3002<\/p>\n<p><strong>app\/controllers\/answers_controller.rb<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059<\/p>\n<p>def update\u306e\u2193\u306b\u4f5c\u6210<\/p>\n<pre><strong>def destroy<\/strong>\r\n<strong>  @question = Question.find(params[:question_id])<\/strong>\r\n<strong>  @answer = @question.answers.find(params[:id])<\/strong>\r\n<strong>  @answer.destroy<\/strong>\r\n<strong>    redirect_to question_path(@question), notice: 'Deleted!'<\/strong>\r\n<strong>end<\/strong>\r\n\r\n<strong>@question = Question.find(params[:question_id]<\/strong>\u2190question_id\u306b\u3057\u306a\u3044\u3068\r\n<strong>AnswersController\uff03destroy\u306eActiveRecord :: RecordNotFound<\/strong>\r\n\u300cid\u300d= 9\u306e\u8cea\u554f\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\r\n\u2191\u306e\u3088\u3046\u306b\u306a\u308b\u306e\u3067\u6ce8\u610f\u3057\u307e\u3059\u3002<\/pre>\n<h3>36\uff1aQuestionsController\u306e\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0<\/h3>\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>class QuestionsController &lt; ApplicationController<\/strong>\r\n\r\n<strong>before_action :set_question, only: [:show, :edit, :update, :destroy]<\/strong>\r\n\r\n<strong>  def index<\/strong>\r\n<strong>    @questions = Question.all<\/strong>\r\n<strong>  end<\/strong>\r\n\r\n<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>\r\n\r\n<strong>  def new<\/strong>\r\n<strong>    @question = Question.new<\/strong>\r\n<strong>  end<\/strong>\r\n\r\n<strong>  def create<\/strong>\r\n<strong>    @question = Question.new(question_params)<\/strong>\r\n<strong>     if @question.save<\/strong>\r\n<strong>      redirect_to root_path, notice: 'success!'<\/strong>\r\n<strong>     else<\/strong>\r\n<strong>    flash[:alert] = 'Save error!'<\/strong>\r\n<strong>   render :new<\/strong>\r\n<strong>  end<\/strong>\r\n<strong> end<\/strong>\r\n\r\n<strong>  def edit<\/strong>\r\n<strong>  # @question = Question.find(params[:id])<\/strong>\r\n<strong>  end<\/strong>\r\n\r\n<strong>  def update<\/strong>\r\n<strong>  # @question = Question.find(params[:id])<\/strong>\r\n<strong>     if @question.update(question_params)<\/strong>\r\n<strong>      redirect_to root_path, notice: 'Success!'<\/strong>\r\n<strong>     else<\/strong>\r\n<strong>     flash[:alert] = 'Save error!'<\/strong>\r\n<strong>     render :edit<\/strong>\r\n<strong>  end<\/strong>\r\n<strong> end<\/strong>\r\n\r\n<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>\r\n \r\n<strong>  private<\/strong>\r\n\r\n<strong>  def set_question<\/strong>\r\n<strong>    @question = Question.find(params[:id])<\/strong>\r\n<strong>  end<\/strong>\r\n\r\n<strong>  def question_params<\/strong>\r\n<strong>    params.require(:question).permit(:name, :title, :content)<\/strong>\r\n<strong>  end<\/strong>\r\n<strong>end<\/strong><\/pre>\n<h3>37\uff1a\u30c7\u30d7\u30ed\u30a4\u306e\u6e96\u5099<\/h3>\n<p><strong>Gemfile<\/strong>\u306b\u4ee5\u4e0b\u306e\u8a18\u8ff0\u3092\u8ffd\u52a0<\/p>\n<p><strong>gem &#8216;sqlite3&#8217;, &#8216;~&gt; 1.3.6&#8217;<\/strong>\u3092\u2193\u306b\u30ab\u30c3\u30c8\u30da\u30fc\u30b9\u30c8<\/p>\n<pre><strong>group :development, :test do<\/strong>\r\n<strong># Call 'byebug' anywhere in the code to stop execution and get a debugger console<\/strong>\r\n<strong>  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]<\/strong>\r\n<strong>  gem 'sqlite3', '~&gt; 1.3.6'<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>\u3053\u308c\u3067\u958b\u767a\u3068\u30c6\u30b9\u30c8\u306e\u6642\u306e\u307f\u4f5c\u52d5\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre><strong>group :production do<\/strong>\r\n<strong>  gem 'pg', '~&gt; 0.18.4'<\/strong>\r\n<strong>end<\/strong><\/pre>\n<p>\u672c\u756a\u74b0\u5883\u306e\u3068\u304d\u3067rails_projects\u306e\u5b9f\u884c\u3059\u308b\u3068\u304d\u306b\u306f<br \/>\n\u3053\u306eGem\u3092\u8aad\u307f\u8fbc\u3080\u3068\u3044\u3046\u8a2d\u5b9a<strong>pg<\/strong>\u306f\u30dd\u30b9\u30b0\u30ec(PostgreSQL)\u3092\u4f7f\u3046\u305f\u3081\u306egem<\/p>\n<pre><strong>bundle install --without production<\/strong><\/pre>\n<p>\u3053\u3046\u3059\u308b\u3053\u3068\u3067production\u4ee5\u5916\u306egem\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u304d\u307e\u3059\u3002<br \/>\n\u30bf\u30fc\u30df\u30ca\u30eb\u3067\u306fDevelopment\u30e2\u30fc\u30c9\u3067\u52d5\u3044\u3066\u3044\u307e\u3059\u3002<br \/>\nproduction\u30e2\u30fc\u30c9\u3067\u52d5\u304f\u306e\u306fHeroku\u306e\u30b5\u30fc\u30d0\u30fc\u4e0a\u3067\u3059\u3002<\/p>\n<p><strong>less .bundle\/config<\/strong>\u3067<br \/>\nBUNDLE_WITHOUT: &#8220;production&#8221;\u8a2d\u5b9a\u304c\u66f8\u304b\u308c\u308b\u306e\u3067<br \/>\n\u4eca\u5f8cbundle install\u3059\u308b\u6642\u306fbundle install\u3067\u3088\u304f\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u4ed8\u3051\u306a\u304f\u3066\u3082\u5927\u4e08\u592b\u3067\u3059\u3002<\/p>\n<p><strong>database.yml<\/strong>\u306e\u8a2d\u5b9a\u30a4\u30f3\u30c7\u30f3\u30c8\u304c\u91cd\u8981\u306a\u3053\u3068\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\uff01<br \/>\n<strong>config\/database.yml<\/strong>\u306b\u3066\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>production:<\/strong>\r\n<strong>  &lt;&lt;: *default<\/strong>\r\n<strong>  adapter: postqresql<\/strong>\r\n<strong>  encoding: unicode<\/strong><\/pre>\n<p>\u30dd\u30b9\u30b0\u30ec\u306b\u63a5\u7d9a\u3059\u308b\u3068\u3044\u3046\u8a2d\u5b9a<br \/>\n\u65e5\u672c\u8a9e\u3082\u4f7f\u3048\u308bunicode\u306b\u3059\u308b\u3068\u3044\u3046\u8a2d\u5b9a<br \/>\nasset\u306e\u30d7\u30ea\u30b3\u30f3\u30d1\u30a4\u30eb\u3092\u6709\u52b9\u306b\u3059\u308b\u3068\u3044\u3046\u8a2d\u5b9a<\/p>\n<p><strong>config\/environment\/production.rb<\/strong>\u3067\u2193\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>config.assets.compile = true<\/strong><\/pre>\n<p>\u2191\u30a2\u30bb\u30c3\u30c8\u306e\u30d7\u30ea\u30b3\u30f3\u30d1\u30a4\u30eb\u3067\u306fCSS\u3084JS\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u4e00\u3064\u306b\u3057\u3066\u5727\u7e2e\u3057\u305f\u308a<br \/>\n\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u3092\u6574\u7406\u3057\u305f\u308a\u3057\u3066\u30e6\u30fc\u30b6\u30fc\u304c<br \/>\n\u9ad8\u901f\u306b\u30da\u30fc\u30b8\u3092\u8aad\u307f\u8fbc\u3081\u308b\u3088\u3046\u306b\u30c7\u30d7\u30ed\u30a4\u6642\u306b\u6e96\u5099\u3092\u884c\u3044\u307e\u3059\u3002<\/p>\n<p><strong>config\/routes.rb<\/strong>\u306b\u3066\u30eb\u30fc\u30c8\u30da\u30fc\u30b8\u304c\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3057\u307e\u3059\u3002\u306a\u3044\u3068\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n<strong>root &#8216;questions#index<\/strong>&#8216;<\/p>\n<h3>38:Git\u306e\u8a2d\u5b9a<\/h3>\n<p>\u2460 &#8211; Git : global config\u306e\u8a2d\u5b9aGit\u3067\u3001\u500b\u4eba\u306e\u8b58\u5225\u60c5\u5831\u3092\u767b\u9332\u3059\u308b\u5fc5\u8981\u304c\u3042\u30ea\u307e\u3059\u3002<\/p>\n<pre><strong>git config --global user.name \"suzu\"<\/strong>\r\n<strong>git config --global user.email bou@example.com\r\n#git\u3092\u521d\u671f\u5316<\/strong>\r\n<strong>git init<\/strong><\/pre>\n<p>\u7ba1\u7406\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u9078\u629e\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\n\u3053\u306e\u5834\u5408gitignore\u30d5\u30a1\u30a4\u30eb\u3092\u9664\u3044\u305f\u5168\u3066(qanda\u30d5\u30a1\u30a4\u30eb\u914d\u4e0b)<\/p>\n<pre><strong>git add -A<\/strong><\/pre>\n<p>show hidden\u30d5\u30a1\u30a4\u30eb\u3067gitignore\u30d5\u30a1\u30a4\u30eb\u306f\u898b\u308b\u3053\u3068\u304c\u3067\u304d\u308b<br \/>\n\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u3064\u3051\u3066\u30b3\u30df\u30c3\u30c8\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>git add -A<\/strong>\r\n<strong>git commit -m \"Initial commit\"<\/strong>\r\n\uff03\u30d5\u30a1\u30a4\u30eb\u3092\u66f4\u65b0\u3057\u305f\u3089\u6bce\u56de\u884c\u3046\u3002\r\n<strong>git commit -m \"Initial commit\"<\/strong><\/pre>\n<h3>39\uff1aHeroku CLI\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n<p>Cloud9\u306a\u3069\u304b\u3089Heroku\u3092\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306e\u30c4\u30fc\u30eb<br \/>\n<strong>CLI(Command Line Interface)<\/strong><\/p>\n<p><strong>~\/environment<\/strong>\u3067\u30bf\u30fc\u30df\u30ca\u30eb\u3067\u4ee5\u4e0b\u3092\u30b3\u30de\u30f3\u30c9\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>curl -OL https:\/\/cli-assets.heroku.com\/heroku-linux-x64.tar.gz<\/strong><\/pre>\n<p>\u3053\u306eURL\u3067\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u2191\u3059\u308b\u3068\u3044\u3046\u610f\u5473<\/p>\n<pre><strong>ls<\/strong>\r\n<strong>heroku-linux-x64.tar.gz rails_projects README.mdruby_projects<\/strong><\/pre>\n<p>\u5727\u7e2e\u30d5\u30a1\u30a4\u30eb\u3092\u89e3\u51cd\u3057\u307e\u3059\u3002\u2193<\/p>\n<pre><strong>tar zxf heroku-linux-x64.tar.gz<\/strong>\r\n\r\n<strong>ls<\/strong>\r\n<strong>heroku heroku-linux-x64.tar.gz rails_projects README.md ruby_projects<\/strong>\r\n\r\n<strong>ls heroku<\/strong>\r\n<strong>bin lib node_modules package.json yarn.lock<\/strong>\r\n<strong>CHANGELOG.md LICENSE oclif.manifest.json README.md<\/strong><\/pre>\n<p>heroku\u3092\u79fb\u52d5\u3057\u307e\u3059\u3002<\/p>\n<p><strong>sudo mv heroku \/usr\/local<\/strong><\/p>\n<p>\u4e00\u822c\u7684\u306b\u30b7\u30b9\u30c6\u30e0\u7ba1\u7406\u8005\u304c\u2191\u81ea\u5206\u3067\u30b3\u30f3\u30d1\u30a4\u30eb\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5834\u6240<\/p>\n<pre><strong>ls \/usr\/local\/heroku<\/strong>\r\n<strong>bin lib node_modules package.json yarn.lock<\/strong>\r\n<strong>CHANGELOG.md LICENSE oclif.manifest.json README.md\r\necho 'PATH=\/usr\/local\/heroku\/bin:$PATH' &gt;&gt; $HOME\/.bash_<\/strong>\r\n<strong>echo 'PATH=\/usr\/local\/heroku\/bin:$PATH' &gt;&gt; $HOME\/.bash_profile<\/strong>\r\n<strong>Path=\/usr\/local\/heroku\/bin:$PATH\uff03\u2190<\/strong>\u3068\u3044\u3046\u6587\u5b57\u5217\u3092\r\n\u30e6\u30fc\u30b6\u30fc\u30db\u30fc\u30e0\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3042\u308b<strong>.bash_profile<\/strong>\u3068\u8a00\u3046\u30d5\u30a1\u30a4\u30eb\u306e\u5185\u5bb9\u306b\u8ffd\u8a18<\/pre>\n<p><strong>.bash_profile<\/strong>\u3068\u306f\uff1f\u30b7\u30a7\u30eb\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u3053\u3068\u3002<\/p>\n<p>\u30b7\u30a7\u30eb\u306f\u4eba\u9593\u304b\u3089\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u306b\u547d\u4ee4\u3092\u4f1d\u3048\u308b\u305f\u3081\u306e\u4ed5\u7d44\u307f<\/p>\n<p>\u8a2d\u5b9a\u3057\u305f\u30d1\u30b9\u306e\u53cd\u6620\u2193<\/p>\n<p><strong>source $HOME\/.bash_profile<\/strong><br \/>\n<strong>function<\/strong><br \/>\n\u2191\u306e\u3088\u3046\u306b\u306a\u308c\u3070OK<\/p>\n<p>\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u78ba\u8a8d<\/p>\n<pre><strong>heroku -v<\/strong>\r\n<strong>heroku\/7.39.1 linux-x64 node-v12.13.0<\/strong><\/pre>\n<p>\u2191\u306e\u3088\u3046\u306b\u306a\u308c\u3070OK<br \/>\n\u5727\u7e2e\u30d5\u30a1\u30a4\u30eb\u524a\u9664<\/p>\n<p><strong>rm -f heroku-linux-64.tar.gz<\/strong><\/p>\n<h3>40: Rails\u30a2\u30d7\u30ea\u3068Heroku\u306e\u95a2\u9023\u4ed8\u3051<\/h3>\n<pre><strong>cd rails_projects\/qanda\/<\/strong><\/pre>\n<p>Heroku\u306b\u30ed\u30b0\u30a4\u30f3<\/p>\n<pre><strong>heroku login --interactive<\/strong><\/pre>\n<p>\u30e1\u30a2\u30c9\u3068\u30d1\u30b9\u3067\u30ed\u30b0\u30a4\u30f3<\/p>\n<p>Rails\u30a2\u30d7\u30ea\u3068Heroku\u306e\u95a2\u9023\u4ed8\u3051\u3092\u3059\u308b<\/p>\n<pre><strong>heroku create qanda-\u30e6\u30fc\u30b6\u30fc\u540d<\/strong><\/pre>\n<p>\u30a2\u30d7\u30ea\u540d\u306f\u5165\u529b\u3057\u306a\u3044\u3068\u30c7\u30d5\u30a9\u30eb\u30c8\u306b\u306a\u308b(\u601d\u3044\u3064\u304b\u306a\u3051\u308c\u3070\u4f7f\u3046)<br \/>\nURL\u306b\u3082\u4f7f\u308f\u308c\u308b\u306e\u3067\u6ce8\u610f\uff01\u8a18\u53f7&amp;\u3084_\u3067\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u306e\u3067<br \/>\n\u51fa\u6765\u308b\u3060\u3051\u4f7f\u308f\u306a\u3044\u307b\u3046\u304c\u7121\u96e3\uff1f<\/p>\n<h3>41:\u30c7\u30d7\u30ed\u30a4<\/h3>\n<pre><strong>cd ~\/environment\/rails_projects\/qanda<\/strong>\r\n\r\n<strong>git push heroku master<\/strong><\/pre>\n<p>\u3053\u306e\u8a18\u8ff0\u3060\u3051\u3067\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u30b5\u30fc\u30d0\u30fc\u306b\u30c7\u30d7\u30ed\u30a4\u3057\u3066\u304f\u308c\u308b<\/p>\n<p>\u6b21\u306b\u30c7\u30fc\u30bf\u3079\u30fc\u30b9\u306e\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u884c\u3046<br \/>\n\u958b\u767a\u74b0\u5883\u3067\u4f7f\u3063\u3066\u3044\u305f\u30b3\u30de\u30f3\u30c9\u306bheroku run\u3092\u3064\u3051\u308b<\/p>\n<pre><strong>heroku run rails db:migrate<\/strong><\/pre>\n<p>URL\u3092\u78ba\u8a8d<\/p>\n<p><strong>heroku apps:info<\/strong><br \/>\n<strong>Web URL: https:\/\/qanda-\u30e6\u30fc\u30b6\u30fc\u540d.herokuapp.com\/<\/strong><\/p>\n<p>URL\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u30c6\u30b9\u30c8\u3057\u3088\u3046\uff01<\/p>\n<h3>42\uff1a\u30a2\u30d7\u30ea\u306e\u524a\u9664\u65b9\u6cd5<\/h3>\n<p>\u4e0d\u5fc5\u8981\u306b\u958b\u767a\u3057\u305f\u30a2\u30d7\u30ea\u3092\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u4e0a\u306b\u516c\u958b\u3057\u305f\u307e\u307e<br \/>\n\u653e\u7f6e\u3059\u308b\u306e\u306f\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u4e0a\u307e\u305a\u3044\u306e\u3067\u30a2\u30d7\u30ea\u306e\u524a\u9664\u3092\u884c\u3046<\/p>\n<p>heroku apps:info\u3067\u30a2\u30d7\u30ea\u540d\u3092\u898b\u308b<br \/>\n=== qanda-\u30e6\u30fc\u30b6\u30fc\u540d<\/p>\n<p>\u524a\u9664\u3059\u308b\u30b3\u30de\u30f3\u30c9&#8211;app \u30a2\u30d7\u30ea\u540d<\/p>\n<p><strong>heroku apps:destroy &#8211;app qanda-\u30e6\u30fc\u30b6\u30fc\u540d<\/strong><\/p>\n<p>&gt; qanda-\u30e6\u30fc\u30b6\u30fc\u540d<br \/>\n\u30a2\u30d7\u30ea\u540d\u3067\u524a\u9664<\/p>\n<p>URL\u306b\u30a2\u30af\u30bb\u30b9\u3057\u4f7f\u3048\u306a\u304f\u306a\u3063\u3066\u3044\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u305f\u3089\u5b8c\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uff15\u304b\u3089\u7d9a\u304d\u307e\u3059\u3002 31\uff1aPost\u3092\u62bc\u3057\u305f\u6642\u306e\u4fdd\u5b58\u51e6\u7406\u306e\u958b\u767a rails routes #\u4fdd\u5b58\u3059\u308b\u969b\u306b\u5fc5\u8981\u306a\u60c5\u5831\u3092\u898b\u308b POST \/questions\/:question_id\/answers(.:format) answ [&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-2827","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\/2827","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=2827"}],"version-history":[{"count":9,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2827\/revisions"}],"predecessor-version":[{"id":2850,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/posts\/2827\/revisions\/2850"}],"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=2827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=2827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/suzutukiblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=2827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}