詳細
新規投稿画面から達成日を選択して、達成日まであと〇日。と投稿一覧画面に表示させたいです。
環境
Windows10
cloud9
rails6.1.5
ruby2.6.3
やった事
https://qiita.com/satopin/items/04157574e976517c5d9b
こちらの記事を参考に実装しました。
フォーム画面は表示出来たものの、データを入れて投稿ボタンを押すと一覧画面に遷移せずフォーム画面に留まったままになります。
こちらの記事は時間まで表示していますが、年、月のみフォーム画面から入力して「あと〇日」と日付だけ表示させたいです。
実装コード
ER図
新規投稿フォーム画面
schema.rb
create_table "dream_lists", force: :cascade do |t| t.string "dream", null: false t.string "period", null: false t.text "detail" t.string "category", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" t.datetime "countdown" end
routes.rb
Rails.application.routes.draw do get '/login' => 'sessions#new' post '/login' => 'sessions#create' delete '/logout' => 'sessions#destroy' resources :users root to: 'homes#top' resources :dream_lists resources :plans get '/search_dream_list' => 'dream_lists#search_dream_list' # カテゴリー検索 end
dream_lists_controller.rb
class DreamListsController < ApplicationController def new @dream_list = DreamList.new end def create @dream_list = current_user.dream_lists.new(dream_list_params) if @dream_list.save redirect_to dream_lists_path flash[:notice] = "1つ夢が追加されました。" else render :new end end def index @dream_lists = current_user.dream_lists end def show @dream_list = current_user.dream_lists.find(params[:id]) end <!--edit,upsate,destroy省略--> private def dream_list_params params.require(:dream_list).permit(:dream, :period, :detail, :image, :category, :countdown) end end
new.html.erb
<div class="container mt-4 mb-4"> <div class="card mx-auto col-lg-8 shadow p-3 bg-white"> <h2>New Dream</h2> <%= form_with model: @dream_list,local: true do |form| %> <div class="ml-3 mt-3"> <%= render "dream_lists/form", form: form %> <div class="mt-3 text-center"> <%= form.submit "投稿", class: "btn btn-sm btn-secondary" %> </div> </div> <% end %> </div> </div>
パーシャルファイル
dream_lists/_form.html.erb
<!--省略--> <div class="row mt-3"> <div class="col-md-4"> <%= form.label :countdown , "達成日 :", class: "form_label" %> </div> <div class="col-md-4"> <%= form.hidden_field :countdown, :id => "countdown.id" %> <input type="text" id="userYear" >年 <input type="text" id="userMonth">月 <input type="text" id="userDate" >日 </div> <!--省略--> </div>
表示場所コード
dream_lists/index.html.erb
<div class="container mt-4 mb-4"> <div class="text-right"> <%= link_to "+dream", new_dream_list_path, class: "btn btn-secondary btn-sm" %> </div> <h2>Many Dreams</h2> <div class="row"> <div class="col-md-9"> <table class="table table-striped text-white"> <!--省略--> <tbody> <% @dream_lists.each do |dream_list| %> <tr> <td><%= check_box_tag '', '' %></td> <td><%= link_to dream_list.dream, dream_list_path(dream_list.id) %></td> <td> <p id="CountDownTime" ></p> <script> function showCountdown() { // 現在日時を数値に変換 var nowDate = new Date(); var dnumNow = nowDate.getTime(); // 指定日時を数値に変換 var inputYear = document.getElementById("userYear").value; var inputMonth = document.getElementById("userMonth").value - 1; var inputDate = document.getElementById("userDate").value; var dnumTarget = targetDate.getTime(); // 引き算して残日数を計算 var diffMSec = dnumTarget - dnumNow; var diffDays = diffMSec / ( 1000 * 60 * 60 * 24 ); var showDays = Math.ceil( diffDays ); // 小数点以下を切り上げる // 表示 var Msg; if( showDays >= 0 ) { Msg = "あと " + showDays + "日"; } else { Msg = "達成日は " + (showDays * -1) + "日前に過ぎました。"; } document.getElementById("CountDownTime").innerHTML = Msg; document.getElementById("countdown.id").value = targetDate; } </script> </td> <td> <!--省略--> </div>
変更後のcord
schema.rb
create_table "dream_lists", force: :cascade do |t| t.string "dream", null: false t.date "period", null: false ←こちらをdata型periodに変更しました。 t.text "detail" t.string "category", null: false t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.integer "user_id" end
dream_lists_controller
class DreamListsController < ApplicationController require 'date' def new @dream_list = DreamList.new end def create @dream_list = current_user.dream_lists.new(dream_list_params) if @dream_list.save @dream_list.update(period: period_get) redirect_to dream_lists_path flash[:notice] = "1つ夢が追加されました。" else render :new end end def index @dream_lists = current_user.dream_lists.page(params[:page]) end def show @dream_list = current_user.dream_lists.find(params[:id]) end def edit @dream_list = current_user.dream_lists.find(params[:id]) end def update @dream_list = current_user.dream_lists.find(params[:id]) if @dream_list.update(dream_list_params) redirect_to dream_list_path(@dream_list.id) flash[:notice] = "更新に成功しました。" else render :edit end end def destroy @dream_list = current_user.dream_lists.find(params[:id]) @dream_list.destroy redirect_to dream_lists_path flash[:notice] = "1つ夢を削除しました。" end def search_dream_list @dream_list = DreamList.new @dream_lists = DreamList.search(params[:keyword]) end private def dream_list_params params.require(:dream_list).permit(:dream, :period, :detail, :image, :category) end def period_get date = params[:dream_list][:period] Date.new date["period(1i)"].to_i,date["period(2i)"].to_i,date["period(3i)"].to_i end ↑62行目はこちらになります。 end
new.html.erb
<div class="container mt-4 mb-4"> <div class="card mx-auto col-lg-8 shadow p-3 bg-white"> <h2>New Dream</h2> <%= render "dream_lists/error_message", dream_list: @dream_list %> <%= form_with model: @dream_list do |form| %> ←form_withはこちらです <div class="ml-3 mt-3"> <%= render "dream_lists/form", form: form %> <div class="mt-3 text-center"> <%= form.submit "投稿", class: "btn btn-sm btn-secondary" %> </div> </div> <% end %> </div> </div>
_form.html.erb
<div class="col-md-4"> <%= form.label :period , "達成日 :", class: "form_label" %> </div> <div class="col-md-4"> <%= form.date_select(:period, start_year: 2022, date_separator: '/') %> ←こちらをdate_selectに変更しました。 <!--<input type="text" id="userYear" >年 <input type="text" id="userMonth">月 <input type="text" id="userDate" >日--> </div>
index.html.erb
<% @dream_lists.each do |dream_list| %> <tr> <td><%= check_box_tag '', '' %></td> <td><%= link_to dream_list.dream, dream_list_path(dream_list.id) %></td> <td> <%= dream_list.period %> ←編集済みのcodeです <td> <%= link_to edit_dream_list_path(dream_list) do %> <i class="fas fa-edit"></i> <% end %> <%= link_to dream_list_path(dream_list), method: :delete, data: { confirm: '削除しますか?' } do %> <i class="fas fa-eraser"></i> <% end %> </td> </tr> <% end %>
変更点の詳細
・カラムをdate型periodに変更しました。
・form_withからdate_selectタグを使用して日付をindex.html.erbに表示することに変更しました。
・dream_lists_controllerにcordを追加しました。
※一旦Javascript での動的な実装は中断し、form_with画面から投稿したデータをindex.html.erbに表示させたいです。
状況
form_withからデータを追加して送信すると、以下のエラーが表示されます。
値がnilになっていますが、送信後のdbには格納されているみたです。
Parameters: {"authenticity_token"=>"[FILTERED]", "dream_list"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00007f324c19c190 @tempfile=#<Tempfile:/tmp/RackMultipart20220504-11279-1he4vwi.jpg>, @original_filename="dummy_image.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"dream_list[image]\"; filename=\"dummy_image.jpg\"\r\nContent-Type: image/jpeg\r\n">, "dream"=>"aaa", "period(1i)"=>"2022", "period(2i)"=>"5", "period(3i)"=>"4", "detail"=>"ああああ", "category"=>"仕事"}, "commit"=>"投稿"}
Date.new date["period(1i)"].to_i,date["period(2i)"].to_i,date["period(3i)"].to_i
の記述の仕方が間違っているのでしょうか?
