select_tagで通知の表示開始期間、表示終了期間を指定し、表示期間内であればindexアクションで通知を表示する機能を作りたいです。しかし、select_tagで選択した情報をpostで送信してもdbにデータが入りません。おそらくmodelの記述が間違っていると思われます。
動作する記述が思いつかないのでご教授いただけると幸いです。
start_dateとend_dateのデータ型はdatetimeです。
HTML
1form 2 3<%= form_for(@caution) do |f| %> 4<div> 5 <label class="control-label" for="">表示開始日時</label> 6 <%= f.datetime_select :start_date, {}, class: 'form-control' %> 7</div> 8<div> 9 <label class="control-label" for="">表示終了日時</label> 10 <%= f.datetime_select :end_date, {}, class: 'form-control' %> 11</div> 12<div> 13 <label class="control-label" for="">タイトル</label> 14 <%= f.text_field :subject, class: 'form-control' %> 15</div> 16<div> 17 <label class="control-label" for="">本文</label> 18 <%= f.text_area :body, class: 'form-control' %> 19</div> 20<div> 21 <%= f.submit '登録・編集', class: 'btn btn-primary' %> 22</div> 23<% end %>
HTML
1index 2<% if @cautions.present? %> 3 <% @cautions.each do |caution| %> 4 <%= link_to "#{caution.subject}", edit_caution_path %> 5 <%= link_to "#{caution.body}", edit_caution_path %> 6 <% end %> 7<% end %> 8 9<%= link_to "通知作成", "/cautions/new" %> 10
Ruby
1controller 2def new 3 @caution = Caution.new 4 end 5 6 def create 7 @caution = Caution.new(start_date: params[:start_date], end_date: params[:end_date], 8 subject: params[:subject], body: params[:body], cosmo_id: params[:cosmo_id]) 9 @caution.cosmo_id = @current_operator.cosmo_id 10 @caution.save 11 redirect_to("/") 12 flash[:notice] = "通知を作成しました" 13 end 14 15 def index 16 @cautions = Caution.find_by_time_now 17 end
Ruby
1model 2 3require 'date' 4 5 def self.find_by_time_now 6 where("end_date - start_date") 7 end
回答1件
あなたの回答
tips
プレビュー