简单整数字段保存为 nil

发布于 2024-10-19 20:44:49 字数 5458 浏览 2 评论 0原文

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

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

场地部分:

<%= 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 更改发生了什么,但它工作正常并关联到正确的用户。

感谢您的帮助!

I recently added a new mapicon_id field to my venue records which displays all availiable icons in a dropdown on new and edit. Each record also has top and left integer fields which allow me to give coords for absolute positioning of the icons using javascript.

Before I added the new mapicon_id field the top and left fields worked fine but now the left field wont save. You can edit the field or create a new record with a left value and click save, it will then give the successfull flash message and redirect correctly but the field will be blank.

venue partial:

<%= 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>

edit and new:

<%= 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 %>

development log on editing a record with top and left values of 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

After editing this record the top field shows 100 but the left field is blank.

venue model:

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 model:

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

UPDATE

I dropped the top and left fields and added toppx and leftpx so the field names aren't so generic, however the problem still exists only now its the toppx field that wont save. Also, If I change the javascript to take the value of the venuetype_id instead of the toppx value it displays correctly. the venuetype_id is 15 and the icon is displayed 15px from the top of the page.

new migration:

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

ANOTHER UPDATE

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>

On a side note I dont know whats happening with the change of user_id there but it works correctly and associates to the correct user.

Thank you for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

善良天后 2024-10-26 20:44:49

Rails 执行的 SQL 查询 (UPDATE ...) 看起来不错,因此必须要么没有被数据库正确保存,要么 Rails 没有从数据库正确获取它。

看一下数据库。执行 SELECT * FROMvenues WHERE id = 45,然后查看该结果是否正确保存在那里。另外,发布您的架构(描述场地)。

The SQL query that Rails did (UPDATE ...) looks fine, so must either not be saved correctly by your database, or Rails doesn't get it back from the DB correctly.

Have a look at the database. Do a SELECT * FROM venues WHERE id = 45, and look at that result to see if it's correctly saved there. Also, post your schema (DESCRIBE venues).

青巷忧颜 2024-10-26 20:44:49

看起来您应该使用 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 %>

Looks like you should use toppx and leftpx istead just top and left:

<%= 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 %>
骄傲 2024-10-26 20:44:49

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

Fixed by sticking a new copy of libmySQL.dll into the ruby bin folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文