GeleiaDeMocoto
GeleiaDeMocoto

Reputation: 161

Date not saving to database when using form_for

I've seen a bunch of questions about this, but when I looked at them, they were always either about using jQuery or from people who were saving their dates as string. In my model :deadline is in date format. I was in doubt whether I should make it date or datetime so maybe that's where I went wrong. Anyway I have this form:

<%= form_for(@task) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <div class="field">
    <%= f.text_field :description, placeholder: "What to do?" %>
  </div>
  <div class="input">
    <%= f.date_field :deadline %>
  </div>
  <%= f.submit "Create Task", class: "btn btn-small btn-default" %>
<% end %>

The form appears as normal, I can pick a date. However, it doesn't save the date to the database. I've tried

<%= f.date_select :deadline %>

as well.

Should I just change :deadline into another type, like maybe leave it as a string and just parse it when it's time to display it?

EDIT: tasks_controller.rb

class TasksController < ApplicationController
  before_action :signed_in_user
  before_action :correct_user,   only: :destroy

  def create
    @task = current_user.tasks.build(task_params)
    if @task.save
      flash[:success] = "Task created!"
      redirect_to root_url
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end
  def destroy
    @task.destroy
    redirect_to root_url
  end

  private

    def task_params
      params.require(:task).permit(:description)
    end

    def correct_user
      @task = current_user.tasks.find_by(id: params[:id])
      redirect_to root_url if @task.nil?
    end
end

Upvotes: 0

Views: 1032

Answers (1)

Prashant4224
Prashant4224

Reputation: 1601

Please update your strong parameters in tasks_controller.rb

def task_params
  params.require(:task).permit(:description, :deadline)
end

Upvotes: 1

Related Questions