我无法同时保存两个日期时间

时间:2016-07-15 19:30:14

标签: ruby-on-rails postgresql datetime simple-form bootstrap-datetimepicker

我正在尝试提交表单并将所有数据保存到数据库中,但是没有保存一列。我有一个表单,您可以在其中设置start_at datetime和end_at datetime。当我单击“创建”按钮时,似乎正在发送两个数据,但只有一个数据被保存到数据库中。起初两者都不会保存,我不知道我做了什么来得到一个保存。

我正在使用Rails 4.2.5.1,ruby 2.3,postgres 9.3,simple_form和bootstrap3-datetimepicker。

以下是与该问题相关的代码,如果有人能够弄清楚我做错了什么我会很感激帮助。

_form.html.erb

<div class="form-group">
    <%= simple_form_for shallow_args(current_user, @image) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <%= f.label            :file %><br>
    <%= f.attachment_field :file, direct: true %>

    <div class="row">
        <%= f.collection_check_boxes :category_ids, Category.all, :id, :name do |cb| %>
        <% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text}%>
        <% end %>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">
                    <%= label_tag :start_at, 'StartTime' %>
                    <div class="input-group date" id="datetimepicker">
                        <%= f.text_field :start_at, class: 'form-control' %>
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <%= label_tag :end_at, 'End of Image run' %>
                    <div class="input-group date" id="datetimepicker1">
                        <%= f.text_field :end_at, class: 'form-control' %>
                        <span class="input-group-addon">
                            <span class="glyphicon glyphicon-calendar"></span>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="actions">
        <%= f.submit %>
    </div>
    <% end %>
</div>

资产/ images.coffee

$(document).on "turbolinks:load", ->
$('#datetimepicker').datetimepicker()
$('#datetimepicker1').datetimepicker()

images_controller.rb

# GET /images/new
def new
    @image = current_user.images.build
end
# POST /images
# POST /images.json
def create
    @image = current_user.images.build(image_params)
    respond_to do |format|
        if @image.save
            format.html { redirect_to @image, notice: 'Image was successfully created.' }
            format.json { render :show, status: :created, location: @image }
        else
            format.html { render :new }
            format.json { render json: @image.errors, status: :unprocessable_entity }
        end
    end
end

def image_params
    params.require(:image).permit(:file, :start_at, :end_at, category_ids: [])
end

从我的服务器输出复制传递的参数:

    Started POST "/users/1/images" for 127.0.0.1 at 2016-07-15 14:46:59 -0400
Processing by ImagesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"87O2h62EXrLtJ084/ydt5N7PzjQ+7ezh2xPk0E14VwtZyA/gb9P6yDExfGYdonfZXSDbKnNX7VExk/80Y+7n9w==", "image"=>{"file"=>"{\"id\":\"c4b7b6c51dd3b083e359e4fa2ba9da2023bef8597c1175a2f2f235c31018\",\"filename\":\"c8f98ef4b2acc1754bab905ee8d61afe.jpg\",\"content_type\":\"image/jpeg\",\"size\":33018}", "category_ids"=>["2", "5", ""], "start_at"=>"07/23/2016 2:46 PM", "end_at"=>"10/08/2016 7:46 PM"}, "commit"=>"Create Image", "user_id"=>"1"}
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
  Category Load (0.6ms)  SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (2, 5)
   (0.2ms)  BEGIN
  Category Exists (0.4ms)  SELECT  1 AS one FROM "categories" WHERE ("categories"."name" = 'Funny' AND "categories"."id" != 2) LIMIT 1
  Category Exists (0.3ms)  SELECT  1 AS one FROM "categories" WHERE ("categories"."name" = 'Cool Stuff' AND "categories"."id" != 5) LIMIT 1
  SQL (0.6ms)  INSERT INTO "images" ("file_id", "end_at", "user_id", "width", "height", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["file_id", "117118"], ["end_at", "2016-08-10 19:46:00.000000"], ["user_id", 1], ["width", "564"], ["height", "564"], ["created_at", "2016-07-15 18:46:59.157176"], ["updated_at", "2016-07-15 18:46:59.157176"]]

我的数据库中保存了什么

   2.3.0 :001 > i = Image.last
  Image Load (0.6ms)  SELECT  "images".* FROM "images"  ORDER BY "images"."id" DESC LIMIT 1
 => #<Image id: 2, created_at: "2016-07-15 18:46:59", updated_at: "2016-07-15 18:46:59", user_id: 1, file_id: "117118", height: "564", width: "564", start_at: nil, end_at: "2016-08-10 19:46:00"> 
2.3.0 :002 > 

编辑,请求图像模型:

image.rb

class Image < ActiveRecord::Base
  belongs_to :user

  has_many   :gallery_images, dependent: :destroy
  has_many   :galleries, through: :gallery_images

  has_many   :categories, through: :category_images
  has_many   :category_images

  attachment   :file, type: :image
  validates    :file, presence: true

  before_save  :set_dimensions, if: :file_id_changed?

  def set_dimensions
    fileio = file.to_io
    mm = if fileio.is_a?(StringIO)
           MiniMagick::Image.read(fileio.read)
         else
           MiniMagick::Image.read(file)
         end

    self.width  = mm.width
    self.height = mm.height
  end
end

0 个答案:

没有答案