简单整数字段保存为nil

时间:2011-02-26 11:24:26

标签: ruby-on-rails activerecord null

我最近在我的场地记录中添加了一个新的mapicon_id字段,在新的和编辑的下拉列表中显示所有可用的图标。每条记录还有顶部和左侧整数字段,这些字段允许我使用javascript为图标的绝对定位提供坐标。

在我添加新的mapicon_id字段之前,顶部和左侧字段工作正常,但现在左侧字段不会保存。您可以编辑该字段或使用左侧值创建一个新记录并单击“保存”,然后它将提供成功的Flash消息并正确重定向,但该字段将为空白。

场地部分:

<%= link_to venue do %>
  <div class="venue_partial">

    <div class="venue_icon">
      <%= image_tag venue.venuetype.photo.url(:thumb), :class => 'image' %>
    </div>
  </div>
<% end %>

<%= link_to venue do %>
  <div id="venue_map_icon_<%= venue.id %>" style="position:absolute;"><%= image_tag venue.mapicon.photo.url(:thumb), :class => 'venue_map_icon' %></div>
<% end %>

<script>
  document.getElementById("venue_map_icon_<%= venue.id %>").style.left= "<%= venue.left %>px";
  document.getElementById("venue_map_icon_<%= venue.id %>").style.top= "<%= venue.top %>px";
</script>

编辑和新:

<%= form_for @venue do |f| %>
  <p>name: <br>
  <%= f.text_field :name %></p>

  <p>mapicon: <br>
  <%= f.collection_select(:mapicon_id, Mapicon.all, :id, :name) %>

  <p>top: <br>
  <%= f.text_field :top %></p>

  <p>left: <br>
  <%= f.text_field :left %></p>

  <%= submit_tag %>
<% end %>

开发日志编辑顶部和左侧值为100的记录:

Started GET "/venues/45-test-place/edit" for 127.0.0.1 at 2011-02-26 11:07:10 +0000
  Processing by VenuesController#edit as HTML
  Parameters: {"id"=>"45-test-place"}
  [1m[35mVenue Load (1.0ms)[0m  SELECT `venues`.* FROM `venues` WHERE `venues`.`id` = 45 LIMIT 1
  [1m[36mArea Load (0.0ms)[0m  [1mSELECT `areas`.* FROM `areas`[0m
  [1m[35mVenuetype Load (0.0ms)[0m  SELECT `venuetypes`.* FROM `venuetypes`
  [1m[36mMapicon Load (1.0ms)[0m  [1mSELECT `mapicons`.* FROM `mapicons`[0m
Rendered venues/edit.html.erb within layouts/application (108.0ms)
Completed 200 OK in 150ms (Views: 127.0ms | ActiveRecord: 2.0ms)


Started POST "/venues/45-test-place" for 127.0.0.1 at 2011-02-26 11:07:16 +0000
  Processing by VenuesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"By8blc3esfE6A8tnfOmRPn9f4KZb/ctwpjg86La4d1Y=", "venue"=>{"name"=>"test place", "addressline1"=>"", "addressline2"=>"", "addressline3"=>"", "phonenumber"=>"", "area_id"=>"8", "venuetype_id"=>"15", "mapicon_id"=>"1", "top"=>"100", "left"=>"100"}, "commit"=>"Save changes", "id"=>"45-test-place"}
  [1m[35mVenue Load (1.0ms)[0m  SELECT `venues`.* FROM `venues` WHERE `venues`.`id` = 45 LIMIT 1
  [1m[36mSQL (0.0ms)[0m  [1mBEGIN[0m
  [1m[35mAREL (0.0ms)[0m  UPDATE `venues` SET `top` = 100, `left` = 100, `updated_at` = '2011-02-26 11:07:16' WHERE `venues`.`id` = 45
  [1m[36mSQL (30.0ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/venues.45-test-place
Completed 302 Found in 65ms

编辑此记录后,顶部字段显示100,但左侧字段为空。

场地模型:

class Venue < ActiveRecord::Base
  belongs_to :user
  has_many :reviews
  belongs_to :area
  belongs_to :venuetype
  belongs_to :mapicon

  scope :with_type, lambda { |types|
    types.present? ? where(:venuetype_id => types) : scoped }

  scope :with_area, lambda { |areas|
    areas.present? ? where(:area_id => areas) : scoped }

  def to_param
    "#{id}-#{name.gsub(/\W/, '-').downcase}"
  end

  def self.search(search)
    if search
      where('name LIKE ?', "%#{search}%")
    else
      scoped
    end
  end
end

mapicon模型:

class Mapicon < ActiveRecord::Base
  has_many :venues
  has_attached_file :photo,
    :styles => {
      :thumb=> "100x100>",
      :small  => "150x150>",
      :medium => "300x300>",
      :large =>   "400x400>" },
    :default_url => '/images/noimage.png'
end

更新

我删除了顶部和左侧字段并添加了toppx和leftpx,因此字段名称不是那么通用,但问题仍然存在,只是现在它不会保存的toppx字段。此外,如果我更改javascript以获取venuetype_id的值而不是toppx值,它会正确显示。 venuetype_id为15,图标显示距离页面顶部15px。

新迁移:

class AddToppxAndLeftpxToVenues < ActiveRecord::Migration

  def self.up
    add_column :venues, :toppx, :integer
    add_column :venues, :leftpx, :integer
  end

  def self.down
    remove_column :venues, :toppx
    remove_column :venues, :leftpx
  end
end

另一个更新

irb(main):002:0> v=Venue.new
=> #<Venue id: nil, name: nil, addressline1: nil, addressline2: nil, addressline
3: nil, created_at: nil, updated_at: nil, area_id: nil, user_id: nil, venuetype_
id: nil, phonenumber: nil, toppx: nil, leftpx: nil>
irb(main):003:0> v.name='test23'
=> "test23"
irb(main):004:0> v.leftpx='24'
=> "24"
irb(main):005:0> v.toppx='42'
=> "42"
irb(main):006:0> v.venuetype_id='13'
=> "13"
irb(main):007:0> v.area_id='2'
=> "2"
irb(main):008:0> v.user_id='6'
=> "6"
irb(main):009:0> v.save
=> true
irb(main):010:0> Venue.find(:last)
=> #<Venue id: 55, name: "test23", addressline1: nil, addressline2: nil, address
line3: nil, created_at: "2011-02-28 18:07:42", updated_at: "2011-02-28 18:07:42"
, area_id: 2, user_id: 946706424, venuetype_id: 13, phonenumber: nil, toppx: nil
, leftpx: 24>
irb(main):011:0>

在旁注上我不知道在那里更改了user_id会发生什么,但它可以正常工作并与正确的用户相关联。

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

Rails做的SQL查询(UPDATE ...)看起来很好,因此数据库必须不能正确保存,否则Rails不能正确地从数据库中恢复。

看看数据库。做一个SELECT * FROM场地WHERE id = 45,看看那个结果,看看它是否正确保存在那里。另外,发布您的架构(DESCRIBE场地)。

答案 1 :(得分:0)

您似乎应该使用toppxleftpx而不仅仅是topleft

<%= form_for @venue do |f| %>
  <p>name: <br>
  <%= f.text_field :name %></p>

  <p>mapicon: <br>
  <%= f.collection_select(:mapicon_id, Mapicon.all, :id, :name) %>

  <p>top: <br>
  <%= f.text_field :toppx %></p>

  <p>left: <br>
  <%= f.text_field :leftpx %></p>

  <%= submit_tag %>
<% end %>

答案 2 :(得分:0)

通过将新的libmySQL.dll副本粘贴到ruby bin文件夹中来解决。