2014-05-12 42 views
0

我有一個非常複雜的Ruby on Rails網站。我沒有對它進行編碼,並且對軌道上的Ruby不太瞭解。發佈保存錯誤數據庫表

部署在雙服務器上的站點。

PostgreSQL的9.0 的Redis 2.6.13 紅寶石1.9.3 Ruby on Rails的3

我添加了一個新的表和幾乎複製另一個(控制器,模型,視圖),一切似乎都正常工作,直到我去保存一個新的記錄到數據庫。

此刻我使用php腳本從csv文件手動插入所有記錄到postgres中。

一切工作很好,除了從網站保存帖子..我甚至可以更新通過PHP腳本手動輸入的紅寶石網站上的帖子。


如果我錯過了什麼,可能需要顯示讓我知道,我將發佈什麼是nessary


當我嘗試發佈一個新Hotload在我的網站將其保存到負載表。

正如你所看到的形式正確地發佈:(我會後一步的一切步驟)

Started POST "https://stackoverflow.com/users/ezpost?method=get" for 108.235.52.160 at 2014-05-12 10:21:25 -0500 
2014-05-12 10:21:25 INFO -- Processing by UsersController#ezpost as HTML 
2014-05-12 10:21:25 INFO -- Parameters: {"utf8"=>"â", "authenticity_token"=>"+csx3CBTDOIejupX2YE9Dv3PkFrqR/de7QWD6AqhVic=", "post"=>{"user_id"=>"10181", "origin"=>"springfield, mo", "dest"=>"st louis, mo", "type"=>"hotload", "equipment_id"=>"8", "pickup"=>"2014-05-12", "comments"=>"Posted From LoadMax EZ-Post Form", "hotload"=>"true"}, "commit"=>"Post", "method"=>"get"} 
2014-05-12 10:21:25 INFO -- Redirected to http://loadmax.com/hotloads/1?load%5Bcomments%5D=Posted+From+LoadMax+EZ-Post+Form&load%5Bdest%5D=st+louis%2C+mo&load%5Bequipment_id%5D=8&load%5Bhotload%5D=true&load%5Borigin%5D=springfield%2C+mo&load%5Bpickup%5D=2014-05-12&load%5Buser_id%5D=10181 
2014-05-12 10:21:25 INFO -- Completed 302 Found in 36ms (ActiveRecord: 2.2ms) 
2014-05-12 10:21:25 INFO -- 

Started GET "/hotloads/1?load%5Bcomments%5D=Posted+From+LoadMax+EZ-Post+Form&load%5Bdest%5D=st+louis%2C+mo&load%5Bequipment_id%5D=8&load%5Bhotload%5D=true&load%5Borigin%5D=springfield%2C+mo&load%5Bpickup%5D=2014-05-12&load%5Buser_id%5D=10181" for 108.235.52.160 at 2014-05-12 10:21:25 -0500 
2014-05-12 10:21:25 INFO -- Processing by HotloadsController#show as HTML 
2014-05-12 10:21:25 INFO -- Parameters: {"load"=>{"comments"=>"Posted From LoadMax EZ-Post Form", "dest"=>"st louis, mo", "equipment_id"=>"8", "hotload"=>"true", "origin"=>"springfield, mo", "pickup"=>"2014-05-12", "user_id"=>"10181"}, "id"=>"1"} 
2014-05-12 10:21:26 INFO -- Rendered hotloads/show.html.haml within layouts/application (181.6ms) 
2014-05-12 10:21:26 INFO -- Rendered shared/_navbar.html.haml (4.1ms) 
2014-05-12 10:21:26 INFO -- Rendered shared/_footer.html.haml (0.2ms) 
2014-05-12 10:21:26 INFO -- Completed 200 OK in 225ms (Views: 64.7ms | ActiveRecord: 150.3ms) 
2014-05-12 10:21:26 INFO -- 

步驟1:表

EZPOST(HTML)

<div class='row-fluid'> 
<div class='form-inline form-horizontal'> 
<form accept-charset="UTF-8" action="https://stackoverflow.com/users/ezpost?method=get" id="ez_post" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="+csx3CBTDOIejupX2YE9Dv3PkFrqR/de7QWD6AqhVic=" /></div> 
<input id="post_user_id" name="post[user_id]" type="hidden" value="10181" /> 
<div class='control-group'> 
<div class='control-label'> 
<label class="required" for="origin_Origin">Origin</label> 
</div> 
<div class='controls'> 
<input class="span10" data-autocomplete="/loads/autocomplete_location_cs" id="post_origin" mandatory="mandatory" name="post[origin]" placeholder="City, ST" type="text" value="" /> 
</div> 
</div> 
<div class='control-group'> 
<div class='control-label'> 
<label for="dest_Destination">Destination</label> 
</div> 
<div class='controls'> 
<input class="span10" data-autocomplete="/loads/autocomplete_location_cs" id="post_dest" name="post[dest]" placeholder="City, ST" type="text" value="" /> 
</div> 
</div> 
<div class='control-group'> 
<div class='control-label'> 
<label for="type_Type">Type</label> 
</div> 
<div class='controls'> 
<select class="span10" id="post_type" name="post[type]"><option value="load">Load</option> 
<option value="truck">Truck</option> 
<option value="hotload">Premium</option></select> 
</div> 
</div> 
<div class='control-group'> 
<div class='control-label'> 
<label for="equipment_Equipment">Equipment</label> 
</div> 
<div class='controls'> 
<select class="span10" id="post_equipment_id" name="post[equipment_id]"><option value="1">Auto Carrier</option> 
<option value="2">B-Train</option> 
<option value="47">Blanket Wrap Van</option> 
<option value="42">Flatbed &amp; Tarps or Van</option> 
<option value="9">Flatbed - Sides</option></select> 
</div> 
</div> 
<div class='ez_post'></div> 
<hr> 
<input class="btn btn-primary" name="commit" type="submit" value="Post" /> 
</form> 

<form accept-charset="UTF-8" action="/info/10181/home?hidden=true&amp;method=get" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="+csx3CBTDOIejupX2YE9Dv3PkFrqR/de7QWD6AqhVic=" /></div> 
<div class='post_load' hidden> 
<div class='control-group'> 
<div class='control-label'> 
<label class="required" for="pickup_Pickup">Pickup</label> 
</div> 
<div class='controls'> 
<input class="datepicker span10" id="post_pickup" mandatory="mandatory" name="post[pickup]" placeholder="YYYY-MM-DD" size="30" type="text" /> 
</div> 
</div> 
<input id="post_comments" name="post[comments]" type="hidden" value="Posted From LoadMax EZ-Post Form" /> 
</div> 
<div class='post_hotload' hidden> 
<div class='control-group'> 
<div class='control-label'> 
<label class="required" for="pickup_Pickup">Pickup</label> 
</div> 
<div class='controls'> 
<input class="datepicker span10" id="post_pickup" mandatory="mandatory" name="post[pickup]" placeholder="YYYY-MM-DD" size="30" type="text" /> 
</div> 
</div> 
<input id="post_comments" name="post[comments]" type="hidden" value="Posted From LoadMax EZ-Post Form" /> 
<input id="post_hotload" name="post[hotload]" type="hidden" value="true" /> 
</div> 
<div class='post_truck' hidden> 
<div class='control-group'> 
<div class='control-label'> 
<label class="required" for="available_available">Available</label> 
</div> 
<div class='controls'> 
<input class="datepicker span10" id="post_available" mandatory="mandatory" name="post[available]" placeholder="YYYY-MM-DD" size="30" type="text" /> 
</div> 
</div> 
<input id="post_expiration" name="post[expiration]" type="hidden" value="2014-05-16" /> 
<input id="post_comments" name="post[comments]" type="hidden" value="Posted From LoadMax EZ-Post Form" /> 
</div> 
</form> 

</div> 
</div> 

UserController中:

def ezpost 
    type = params[:post].delete(:type) 
    Resque.enqueue(UserPoster, params[:post]) 
    redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :load => params[:post] 
    end 

Hotload控制器(在這個階段它拉右視圖頁。)

def show 
    if params[:hotload] 
     params[:hotload][:origin] = Location.to_point(params[:hotload][:origin]) 
     params[:hotload][:dest] = Location.to_point(params[:hotload][:dest]) 
     @hotload = Hotload.new(params[:hotload]) 
     @hotload.updated_at = Time.now 
     @hotload.id = -1 
     authorize! :show, @hotload 
     flash.keep[:notice] = 'Please be aware that it could take up to 5 minutes for your hotload to appear in search results.' 
    elsif params[:load] 
     params[:load][:origin] = Location.to_point(params[:load][:origin]) unless params[:load][:origin].blank? 
     params[:load][:dest] = Location.to_point(params[:load][:dest]) unless params[:load][:dest].blank? 
     @hotload = Hotload.new(params[:load]) 
     @hotload.updated_at = Time.now 
     @hotload.id = -1 
     authorize! :show, @hotload 
     flash.keep[:notice] = 'Please be aware that it could take up to 5 minutes for your truck to appear in search results.' 
    else 
     @hotload = Hotload.find(params[:id]) 
     @return_hotloads = Hotload.return_hotloads(@hotload) 
     @return_loads = Load.return_loads(@hotload) 
     authorize! :show, @hotload 
    end 
    end 

工人:user_poster

class UserPoster 
    @queue = :user_posting 

    def self.perform(_post) 
    post_type=Load 
    post_type=Hotload if (_post['hotload']) 
    post_type=Truck if (_post['available']) 
    result=post_type.send("post",_post) 
    Notification.check(result.id, post_type) 
    end 

end 

Hotloads控制器(POST)

def post 
    @hotload = {} 
    #assign hotload to new_hotload unless new_hotload doesn't exist. new_hotload is passed over via ezpost on the home page. 
    unless params[:new_load].blank? 
     params[:new_load].delete(:expiration) 
     params[:new_load].delete(:available) 
     params[:new_load].delete(:type) 
     params[:hotload] = params[:new_load] 
    end 
    @hotload = params[:hotload] 
     unless params[:post].nil? 
     @hotload[:type] = params[:post][:type] unless params[:post][:type].nil? 
     end 
    if params[:new_load].nil? 
     @hotload[:equipment] = params[:equipment][0] unless params[:equipment].blank? 
    else 
     #equipment_id 34 means 'Call for Equipment Type.' 
     params[:hotload][:equipment_id] = 34 
    end 
    if Resque.enqueue(UserPoster,params[:hotload]) 
     flash.keep[:notice] = 'Please be aware that it could take up to 5 minutes for your premium load to appear in search results.' 
    else 
     flash.keep[:error] = 'Something went wrong.' 
    end 
    redirect_to :action => 'show', :id => 1, :hotload => @hotload 
    end 

FULL Hotload模型

class Hotload < ActiveRecord::Base 
    validates :user_id, :uniqueness => {:scope => [:origin, :dest, :equipment_id, :length, :ltl, :pickup, :delivery, :rate, :weight]} 
    attr_accessible :comments, :covered, :delivery, :dest, :equipment_id, :length, :ltl, :origin, :pickup, :rate, :user_id, :weight 
    belongs_to :user 
    has_one :equipment 
    set_rgeo_factory_for_column(:origin, RGeo::Geographic.spherical_factory(:srid => 4326)) 
    set_rgeo_factory_for_column(:dest, RGeo::Geographic.spherical_factory(:srid => 4326)) 
    before_save :add_city_and_state 

    def self.post(post) 
    origin_state = post['origin'][-2,2] 
    origin_city = post['origin'].gsub(%r(, [a-zA-Z]+), '').strip 
    dest_state = post['dest'][-2,2] 
    dest_city = post['dest'].gsub(%r(, [a-zA-Z]+), '').strip 
    origin = Location.first(:conditions => {:city => origin_city.downcase, :state => origin_state.downcase}) 
    dest = Location.first(:conditions => {:city => dest_city.downcase, :state => dest_state.downcase}) 
    if origin.nil? or dest.nil? 
    origin = Location.where("levenshtein(city, '#{origin_city.downcase}') <= 4 AND state = '#{origin_state.downcase}'").first 
    dest = Location.where("levenshtein(city, '#{dest_city.downcase}') <= 4 AND state = '#{dest_state.downcase}'").first 
    end 
    post['origin'] = "POINT (#{origin.coords.x} #{origin.coords.y})" 
    post['dest'] = "POINT (#{dest.coords.x} #{dest.coords.y})" 
    l = Hotload.new(post) 
    k = post.keys 
    k.each do |key| 
     if post[key] = '' 
     post.delete(key) 
     end 
    end 
    l.save! 
    l 
    end 

    def self.suggested_hotloads(user_id) 
    @user = User.find(user_id) 
    now = Time.now.strftime("%F") 
    user_notifier = Notification.where(user_id: @user.id).first 
    user_zip = @user.company.zip 
    user_location = Location.where(zip: user_zip).first 
    @suggested_hotloads = [] 
    begin 
     notifier_hotloads = Hotload.where("origin = '#{user_notifier.origin}' OR dest = '#{user_notifier.dest}' AND pickup > '#{now}'").limit(25).order('id DESC') 
     notifier_hotloads.each {|x| @suggested_hotloads.push x } if notifier_hotloads 
    rescue Exception => e 
     unless user_notifier.blank? 
     if !user_notifier.origin.blank? 
      notifier_hotloads = Hotload.where("origin = '#{user_notifier.origin}' AND pickup > '#{now}'").limit(25).order('id DESC') 
     elsif !user_notifier.dest.blank? 
      notifier_hotloads = Hotload.where("origin = '#{user_notifier.dest}' AND pickup > '#{now}'").limit(25).order('id DESC') 
     end 
     notifier_hotloads.each {|x| @suggested_hotloads.push x } if notifier_hotloads 
     end 
    end 
    begin 
     location_hotloads = Hotload.where("origin = '#{user_location.coords}' OR dest = '#{user_location.coords}' AND pickup > '#{now}'").limit(25).order('id DESC') 
     location_hotloads.each {|x| @suggested_hotloads.push x } if location_hotloads 
    rescue Exception => e 
     puts e unless Rails.env.test? 
    end 
    @recents = Hotload.where("rate > 0 AND pickup > '#{now}'").order('id DESC') 
    @recents.each do |recent| 
     if @suggested_hotloads.count < 50 
     @suggested_hotloads.push recent 
     end 
    end 
    return @suggested_hotloads 
    end 

    def self.return_hotloads(hotload) 
    if hotload.delivery.blank? then hotload.delivery = Time.now end 
    Hotload.where(
     "ST_Distance(origin, ST_GeomFromText('#{hotload.dest}')) <= 100*1609.334 
     AND ST_Distance(dest, ST_GeomFromText('#{hotload.origin}')) <= 100*1609.334 
     AND pickup BETWEEN '#{hotload.delivery}' AND '#{hotload.delivery+3.days}' 
      AND equipment_id = '#{hotload.equipment_id}'" 
    ).limit(5) 
    end 

    private 
    def add_city_and_state 
     origin = Location.city_state(self.origin) 
     dest = Location.city_state(self.dest) 
     if origin == "ANYWHERE" 
     self.origin_city, self.origin_state = nil, nil 
     else 
     self.origin_city = origin[:city] 
     self.origin_state = origin[:state] 
     end 
     if dest == "ANYWHERE" 
     self.dest_city, self.dest_state = nil, nil 
     else 
     self.dest_city = dest[:city] 
     self.dest_state = dest[:state] 
     end 
    end 
end 

試圖讓我的腳本記錄錯誤,但沒有這樣的運氣

class UserPoster 
    @queue = :user_posting 

    def self.perform(_post) 
    post_type=Load 
    post_type=Hotload if (_post['hotload']) 
    @log.error _post['hotload'] 
    post_type=Truck if (_post['available']) 
    @log.error _post['avaliable'] 
    result=post_type.send("post",_post) 
    Notification.check(result.id, post_type) 
    end 
    @log = Logger.new("#{Rails.root}/log/userposter.log") 
    @log.datetime_format = "%F %T" 
end 
+0

你在哪裏卡住了?無法理解你上面解釋的是什麼...你能更具體地 – Dave

+0

我已經添加了新的Hotloads表。出於某種原因,我無法弄清楚爲什麼當我發佈一個新的熱負載時,它將它保存到負載表中。我想這可能在工人的某個地方,因爲我安裝了redis。 – user3568861

+0

一切似乎工作正常,除了保存到錯誤的表 – user3568861

回答

0

在看看你的日誌後:

2014-05-12 10:21:25 INFO -- Parameters: {"load"=>{"comments"=>"Posted From LoadMax EZ-Post Form", "dest"=>"st louis, mo", "equipment_id"=>"8", "hotload"=>"true", "origin"=>"springfield, mo", "pickup"=>"2014-05-12", "user_id"=>"10181"}, "id"=>"1"} 

這可能是因爲你的redirect_to代碼傳遞:loadparams[post]當它應該是:hotload。將它的工作,如果你添加在UserController中的第二備選redirect_to代碼:

def ezpost 
     type = params[:post].delete(:type) 
     Resque.enqueue(UserPoster, params[:post]) 

     if params[:post][:type] == "load" 
      redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :load => params[:post] 
     elsif params[:post][:type] == "hotload" 
      redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :hotload => params[:post] 
     else #truck 
      redirect_to :controller => "#{type}s", :action => 'show', :id => 1, :truck => params[:post] 
     end 
    end 

我沒有測試的代碼,但希望它會幫助

+0

我把它作爲負載傳遞,因爲我使用表單3種不同類型的張貼。負載,熱載荷,卡車。一旦它到達適當的控制器ex熱負荷。我有@hotload = Hotload.new(params [:負載])...但它應該保存之前,它從用戶控制器得到那裏... Resque.enqueue(UserPoster,params [:post]) – user3568861

+0

謝謝澄清,你的Rails代碼確實非常先進。我只是看了RailsCasts上的Resque,這對我來說是全新的。 http://railscasts.com/episodes/271-resque顯然,可能會有一些延遲,但我不確定它在這種情況下是否有任何效果 – jyrkim

+0

發現問題是什麼?那麼我只是爲了防萬一加了卡車來確定ezpost。 – jyrkim

1

你的問題似乎是在perform方法,因爲這就是做出決定的地方,我可以看到的唯一代碼表明瞭Load或Hoatload的分支路徑。它可以像使用_post參數中的符號一樣簡單。嘗試這個。

class UserPoster 
    @queue = :user_posting 

    def self.perform(_post) 
    post_type=Load 
    post_type=Hotload if (_post[:hotload]) 
    post_type=Truck if (_post[:available]) 
    result=post_type.send("post",_post) 
    Notification.check(result.id, post_type) 
    end 

end 

在我看來就像它的設置post_type加載在一開始,然後post_type沒有返回true,所以它最終在年底做Load.post _post

如果不是這樣,你應該努力弄清楚爲什麼_post ['hotload']沒有像預期的那樣返回true。你可以通過Rails.logger.info _post ['hotload']來查看它的值。你可能想要做Rails.logger.info "The expected value is #{_post['hotload']}",所以如果它根本不起作用,你就不會空白。

+0

我無法得到任何價值的記錄器見上面...我試圖.info和.debug和.error – user3568861

+0

順便說一句,張貼一個卡車與上面的代碼一起工作post_type = Truck if(_post ['available'])因此它應該與hotload大致相同 – user3568861

+0

如果你只是做'_post ['hotload']。foo',看看它報告什麼崩潰?你會得到一個NoMethodError。如果它來自無,你知道你並沒有真正傳遞你期望的價值。你也可以嘗試'puts _post ['hotload']'它會輸出到你的服務器控制檯。 – subvertallchris