我有一个非常复杂的Ruby on Rails网站。我没有对它进行编码,也不太了解Ruby on rails。
站点部署在双服务器上。
Postgresql 9.0 redis 2.6.13 红宝石1.9.3 Ruby on rails 3
我添加了一个新表,几乎复制了另一个(控制器,模型,视图),所有内容似乎都正常工作,直到我将新记录保存到数据库。
目前我正在使用csv文件中的php脚本手动将所有记录插入postgres。
除了从网站保存帖子之外,一切都很好。我甚至可以通过php脚本手动输入ruby网站上的帖子更新。
如果我遗失任何可能需要展示的内容,请告诉我,我会发布任何必要的内容
当我尝试在我的网站上发布一个新的Hotload时,它会保存到Loads Table。
你可以看到表格正确发布:(我将逐步发布所有内容)
Started POST "/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="/users/ezpost?method=get" id="ez_post" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><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 & 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&method=get" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><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 Controller :(在此阶段它会拉出正确的View页面。)
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
Workers: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 Controller(帖子)
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 :(得分:1)
你的问题似乎是在perform
方法中,因为那是决定的地方,我能看到的唯一代码就是建议加载或加载的分支路径。它可以像在_post
param中使用符号一样简单。试试这个。
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设置为Load,然后post_type
没有返回True,所以它最终会在Load.post _post
处结束。
如果不是这样,你应该弄清楚为什么_post ['hotload']没有按预期返回true。您可以执行Rails.logger.info _post ['hotload']查看其值。你可能想要Rails.logger.info "The expected value is #{_post['hotload']}"
,所以如果它根本不起作用你就不会得到空行。
答案 1 :(得分: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
代码:load
代码params[post]
为:hotload
时redirect_to
。如果您在UserController中添加了第二个替代 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
代码,它是否会起作用:
{{1}}
我没有测试过代码,但希望它能帮助