我无法将日期字段值存储到数据库中。我使用datepicker选项来选择日期。我可以存储在模型中初始化日期的日期。
用于保存数据的Rails控制台如下:
Started POST "/folders" for 127.0.0.1 at 2015-10-14 16:55:17 +0530
Processing by FoldersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Lflnh4FTwyLdkiSXHvXnzmnTNYwpxIFk1uHl7byXoIPeVaFv2I8ehvZrYx1g8koKnwzYcKJ9zftiHao6x9Grug==", "commit"=>"Save", "folder"=>{"folder_date"=>"2015-10-14", "client_id"=>"5", "employee_id"=>"5", "priority"=>"Normal", "requirements"=>"", "additional_requirements"=>"", "estimated_cost"=>"", "expected_date"=>"10/31/2015", "attachments_attributes"=>{"0"=>{"remote_attachment_url"=>"", "_destroy"=>"false"}}, "notes"=>""}}
文件夹控制器如下:
class FoldersController < ApplicationController
before_action :set_folder, only: [:show, :edit, :update, :destroy]
def table
@folders = Folder.includes(:client).includes(:employee).all
respond_to do |format|
format.json { render json: FoldersDatatable.new(view_context, @folders) }
end
end
def show
end
def assign_index
@folder = Folder.find(params[:folder_id])
end
def assignment
@folder = Folder.find(params[:folder_id])
respond_to do |format|
format.html { render :assignment}
end
end
def assign
respond_to do |format|
if @folder.update(folder_params)
format.html { redirect_to @folder, notice: 'Folder was successfully updated.' }
format.json { render :show, status: :ok, location: @folder }
else
format.html { render :edit }
format.json { render json: @folder.errors, status: :unprocessable_entity }
end
end
end
def new
@folder = Folder.new
@folder.attachments.build
end
def edit
@folder.attachments.build unless @folder.attachments.present?
end
def create
@folder = Folder.new(folder_params)
respond_to do |format|
if @folder.save
format.html { redirect_to @folder, notice: 'Folder was successfully created.' }
format.json { render :show, status: :created, location: @folder }
else
format.html { render :new }
format.json { render json: @folder.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @folder.update(folder_params)
format.html { redirect_to @folder, notice: 'Folder was successfully updated.' }
format.json { render :show, status: :ok, location: @folder }
else
format.html { render :edit }
format.json { render json: @folder.errors, status: :unprocessable_entity }
end
end
end
def destroy
@folder.destroy
respond_to do |format|
format.html { redirect_to folders_url, notice: 'Folder was successfully destroyed.' }
format.json { head :no_content }
end
end
def select
@folders = Folder.where(id: "#{params[:id]}") if params[:id].present?
respond_to do |format|
format.json { render json: FoldersSelect.new(@folders, params) }
end
end
def cancel
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.canceled!
format.html { redirect_to @folder, notice: "Folder was successfully cancelled" }
else
format.html { redirect_to @folder, notice: "Folder was not cancelled" }
end
end
end
def close
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.closed!
format.html { redirect_to @folder, notice: "Folder was successfully closed" }
else
format.html { redirect_to @folder, notice: "Folder was not closed" }
end
end
end
def destroy
@folder.destroy
respond_to do |format|
format.html { redirect_to folders_url, notice: 'Folder was successfully destroyed.' }
format.json { head :no_content }
end
end
def reviewed
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.reviewed!
format.html { redirect_to @folder, notice: 'Folder is reviewed' }
else
format.html { redirect_to @folder, alert: 'Folder status update to reviewed failed due to errors.' }
end
end
end
def accepted
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.accepted!
format.html { redirect_to @folder, notice: 'Folder is accepted' }
else
format.html { redirect_to @folder, alert: 'Folder status update to accepted failed due to errors.' }
end
end
end
def in_progress
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.in_progress!
format.html { redirect_to @folder, notice: 'Folder is in_progress' }
else
format.html { redirect_to @folder, alert: 'Folder status update to in_progress failed due to errors.' }
end
end
end
def wait_approval
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.wait_approval!
format.html { redirect_to @folder, notice: 'Folder is wait_approval' }
else
format.html { redirect_to @folder, alert: 'Folder status update to wait_approval failed due to errors.' }
end
end
end
def wait_comp
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.wait_comp!
format.html { redirect_to @folder, notice: 'Folder is wait_comp' }
else
format.html { redirect_to @folder, alert: 'Folder status update to wait_comp failed due to errors.' }
end
end
end
def outsourced
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.outsourced!
format.html { redirect_to @folder, notice: 'Folder is outsourced' }
else
format.html { redirect_to @folder, alert: 'Folder status update to outsourced failed due to errors.' }
end
end
end
def completed
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.completed!
format.html { redirect_to @folder, notice: 'Folder is completed' }
else
format.html { redirect_to @folder, alert: 'Folder status update to completed failed due to errors.' }
end
end
end
def abandoned
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.abandoned!
format.html { redirect_to @folder, notice: 'Folder is abandoned' }
else
format.html { redirect_to @folder, alert: 'Folder status update to abandoned failed due to errors.' }
end
end
end
def pending
@folder = Folder.find params[:folder_id]
respond_to do |format|
if @folder.payment_pending!
format.html { redirect_to @folder, notice: 'Folder is in payment pending' }
else
format.html { redirect_to @folder, alert: 'Folder status update to payment pending failed due to errors.' }
end
end
end
private
def set_folder
@folder = Folder.find(params[:id])
end
def folder_params
params.require(:folder).permit(:code, :organization_id, :client_id, :vendor_id, :employee_id, :kind, :folder_date, :item, :owner, :assigned_employee_id, :assigned_date,
:party, :status, :notes, :active, :archived, :requirements, :additional_requirements, :priority, :item_name, :item_parts,
:owner_name, :owner_phone, :owner_email, :party_name, :party_phone, :party_email, :estimated_cost, :expected_date,
attachments_attributes: [:id, :organization_id, :attachable_type, :attachable_id,
:attachment, :remote_attachment_url],
components_attributes: [:id, :organization_id, :vendor_id, :folder_id, :default, :kind, :name, :reference, :properties, :status, :notes, :_destroy])
end
end
完整的日志如下:
Started GET "/clients/select.json?s=a&per=10" for 127.0.0.1 at 2015-10-15 10:18:34 +0530
Processing by ClientsController#select as JSON
Parameters: {"s"=>"a", "per"=>"10"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Client Load (0.5ms) SELECT "clients".* FROM "clients" WHERE (clients.name ilike '%a%') ORDER BY clients.name LIMIT 10
Address Load (0.3ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2 LIMIT 1 [["addressable_id", 1], ["addressable_type", "Client"]]
Phone Load (0.3ms) SELECT "phones".* FROM "phones" WHERE "phones"."callable_id" = $1 AND "phones"."callable_type" = $2 ORDER BY "phones"."id" ASC LIMIT 1 [["callable_id", 1], ["callable_type", "Client"]]
Address Load (0.3ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2 LIMIT 1 [["addressable_id", 5], ["addressable_type", "Client"]]
Phone Load (0.4ms) SELECT "phones".* FROM "phones" WHERE "phones"."callable_id" = $1 AND "phones"."callable_type" = $2 ORDER BY "phones"."id" ASC LIMIT 1 [["callable_id", 5], ["callable_type", "Client"]]
Address Load (0.3ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2 LIMIT 1 [["addressable_id", 4], ["addressable_type", "Client"]]
Phone Load (0.4ms) SELECT "phones".* FROM "phones" WHERE "phones"."callable_id" = $1 AND "phones"."callable_type" = $2 ORDER BY "phones"."id" ASC LIMIT 1 [["callable_id", 4], ["callable_type", "Client"]]
Address Load (0.3ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2 LIMIT 1 [["addressable_id", 3], ["addressable_type", "Client"]]
Phone Load (0.3ms) SELECT "phones".* FROM "phones" WHERE "phones"."callable_id" = $1 AND "phones"."callable_type" = $2 ORDER BY "phones"."id" ASC LIMIT 1 [["callable_id", 3], ["callable_type", "Client"]]
Completed 200 OK in 17ms (Views: 10.1ms | ActiveRecord: 3.5ms)
Started GET "/employees/select.json?s=e&per=10" for 127.0.0.1 at 2015-10-15 10:18:37 +0530
Processing by EmployeesController#select as JSON
Parameters: {"s"=>"e", "per"=>"10"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Employee Load (0.7ms) SELECT "employees".* FROM "employees" WHERE (employees.name ilike '%e%') ORDER BY employees.name LIMIT 10
Completed 200 OK in 10ms (Views: 1.6ms | ActiveRecord: 1.7ms)
Started POST "/folders" for 127.0.0.1 at 2015-10-15 10:18:47 +0530
Processing by FoldersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6LTvSiLk2KDZzuNctei3Y8LmB7AaxBPw5SWWaCzVNvobGCmiezgFBPI3pNbL7xqnNDnqTJF9X29R2dm/V5M9ww==", "commit"=>"Save", "folder"=>{"folder_date"=>"2015-10-15", "client_id"=>"5", "employee_id"=>"1", "priority"=>"Normal", "requirements"=>"", "additional_requirements"=>"", "estimated_cost"=>"", "expected_date"=>"10/30/2015", "attachments_attributes"=>{"0"=>{"remote_attachment_url"=>"", "_destroy"=>"false"}}, "notes"=>""}}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Unpermitted parameter: _destroy
(0.2ms) BEGIN
Protokoll::Models::CustomAutoIncrement Load (163.8ms) SELECT "custom_auto_increments".* FROM "custom_auto_increments" WHERE "custom_auto_increments"."counter_model_name" = $1 LIMIT 1 [["counter_model_name", "folder"]]
SQL (0.4ms) UPDATE "custom_auto_increments" SET "counter" = $1, "updated_at" = $2 WHERE "custom_auto_increments"."id" = $3 [["counter", 60], ["updated_at", "2015-10-15 04:48:47.918273"], ["id", 4]]
SQL (17.7ms) INSERT INTO "folders" ("client_id", "employee_id", "folder_date", "notes", "requirements", "additional_requirements", "priority", "status", "created_at", "updated_at", "code") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id" [["client_id", 5], ["employee_id", 1], ["folder_date", "2015-10-15"], ["notes", ""], ["requirements", ""], ["additional_requirements", ""], ["priority", "Normal"], ["status", "opened"], ["created_at", "2015-10-15 04:48:47.747063"], ["updated_at", "2015-10-15 04:48:47.747063"], ["code", "F00060"]]
SQL (111.9ms) INSERT INTO "versions" ("event", "whodunnit", "created_at", "item_id", "item_type") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["event", "create"], ["whodunnit", "1"], ["created_at", "2015-10-15 04:48:47.747063"], ["item_id", 60], ["item_type", "Folder"]]
SQL (7.1ms) INSERT INTO "attachments" ("attachment", "attachable_type", "attachable_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["attachment", nil], ["attachable_type", "Folder"], ["attachable_id", 60], ["created_at", "2015-10-15 04:48:48.095467"], ["updated_at", "2015-10-15 04:48:48.095467"]]
(27.4ms) COMMIT
Redirected to http://localhost:3000/folders/60
Completed 302 Found in 397ms (ActiveRecord: 332.9ms)
Started GET "/folders/60" for 127.0.0.1 at 2015-10-15 10:18:48 +0530
Processing by FoldersController#show as HTML
Parameters: {"id"=>"60"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Folder Load (0.3ms) SELECT "folders".* FROM "folders" WHERE "folders"."id" = $1 LIMIT 1 [["id", 60]]
(0.3ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'manager') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
Rendered shared/actions/_actions.html.haml (4.9ms)
Client Load (0.4ms) SELECT "clients".* FROM "clients" WHERE "clients"."id" = $1 LIMIT 1 [["id", 5]]
Address Load (0.3ms) SELECT "addresses".* FROM "addresses" WHERE "addresses"."addressable_id" = $1 AND "addresses"."addressable_type" = $2 LIMIT 1 [["addressable_id", 5], ["addressable_type", "Client"]]
Phone Load (0.3ms) SELECT "phones".* FROM "phones" WHERE "phones"."callable_id" = $1 AND "phones"."callable_type" = $2 ORDER BY "phones"."id" ASC LIMIT 1 [["callable_id", 5], ["callable_type", "Client"]]
Email Load (2.6ms) SELECT "emails".* FROM "emails" WHERE "emails"."emailable_id" = $1 AND "emails"."emailable_type" = $2 ORDER BY "emails"."id" ASC LIMIT 1 [["emailable_id", 5], ["emailable_type", "Client"]]
Employee Load (0.4ms) SELECT "employees".* FROM "employees" WHERE "employees"."id" = $1 LIMIT 1 [["id", 1]]
Component Load (21.8ms) SELECT "components".* FROM "components" WHERE "components"."folder_id" = $1 [["folder_id", 60]]
Employee Load (0.7ms) SELECT "employees".* FROM "employees"
PaperTrail::Version Load (23.5ms) SELECT "versions".* FROM "versions" WHERE "versions"."item_id" = $1 AND "versions"."item_type" = $2 ORDER BY "versions"."created_at" ASC, "versions"."id" ASC, id asc [["item_id", 60], ["item_type", "Folder"]]
Rendered folders/show.html.haml within layouts/application (133.7ms)
Role Load (0.4ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
Role Load (0.3ms) SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'manager') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]]
Rendered layouts/_navigation.html.haml (3.9ms)
Rendered layouts/_flash.html.haml (0.9ms)
Completed 200 OK in 384ms (Views: 324.8ms | ActiveRecord: 53.4ms)
答案 0 :(得分:0)
通过使用datepicker javascript的日期格式来解决。
// datepicker
$('.datepicker-input').datepicker({
todayBtn: "linked",
todayHighlight: true,
format: 'yyyy-mm-dd'
});