我最近在我的場地記錄中添加了一個新的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 left值:
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
UPDATE
我放棄了頂部和左側領域,並添加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出現的變化發生,但它正常工作和同事給正確的用戶。
謝謝你的幫助!
左邊的字段在'POST'請求日誌中作爲'100'傳遞,所以視圖代碼可能沒問題。模型代碼如何?發佈呢? – sarnold 2011-02-26 11:30:41
@sarnold感謝您的光臨,添加了場地和mapicon模型代碼 – Dave 2011-02-26 11:58:23
我很難過,它看起來很簡單,不會失敗。如何控制器代碼呢?如果沒有別的,添加控制器代碼應該將問題帶到活動列表的頂部,以便那些更喜歡以這種方式查看問題的人... – sarnold 2011-02-27 04:59:22