请多多包涵,因为我是 Rails 和一般编程新手。我对模型、视图、控制器的功能了解非常有限,但我一直在尽可能快地学习。
我无法根据已有的数据生成半静态中间页面。现在它只是一个邮政编码搜索,一个带有结果列表的页面和一个商店页面。我希望它由 State-city-list-shop 构成
在我的数据库中,商店位置已经过地理编码,并且可以通过邮政编码搜索在客户端访问。因此,我正在处理具有通常脚手架类型设置的 Shops 控制器,因此显示视图的 url 看起来像 /shops/1。我想要的是带有面包屑的商店/阿拉斯加/安克雷奇/商店名称。
我没有“州”或“城市”控制器,我认为您必须这样做才能构建路线。
我制作了静态的 State 模板页面,对于每个 State,我都列出了带有类似City链接的城市。我在每个 State 文件夹中都有一个 index.html 文件,但 /shop/state/city 中没有任何内容
我这样做是因为我可以从中得到结果:
/shops/find_shops?=zip=City&Distance=5
如果我能得到/state/city/shops的相同结果,我会很高兴。
因为那时我可以单击该列表中的一家商店,它会将我带到/shops/7499 (:id) 的商店页面,最好有/state/city/shop-name
这是商店控制器- 管理员只是保护公众免于编辑商店地址信息。正如您将看到的,它正在获取一个 csv 文件,解析并保存到数据库中。
def admin_index
#@shops = Shop.find(:all)
@shops = Shop.paginate( :per_page => 35,
:page => params[:page])
@shops_to_geocode = Shop.fetch_not_geocoded
end
def geocode_shops
@shops_to_geocode = Shop.fetch_not_geocoded
cnt = 0
for shop in @shops_to_geocode
shop.geocode_raw_address
shop.save!
end
redirect_to :action => 'admin_index'
end
def upload_file
file_path = params[:file_path]
if file_path
Shop.import( file_path )
end
redirect_to :action => 'admin_index'
end
# GET /shops
# GET /shops.xml
def index
@zip = params[:zip]
@distance = params[:distance]
if @zip && @distance
@shops = Shop.find(:all, :origin => @zip, :conditions => ["distance < ?", @distance])
logger.debug( "found #{@shops.length} shops" )
if @shops.length == 0
geo = GeoKit::Geocoders::MultiGeocoder.geocode( @zip )
errors.add(:address, "Could not Geocode address") if !geo.success
@centerLat, @centerLng = geo.lat,geo.lng if geo.success
else
@centerLat = @shops[0].lat
@centerLng = @shops[0].lng
end
else
@shops = []
geo = GeoKit::Geocoders::IpGeocoder.geocode(request.remote_ip)
if geo.success
@centerLat, @centerLng = geo.lat,geo.lng
else
logger.debug( "unable to geocode remote ip" )
@centerLat = 42
@centerLng = -120
end
end
if @distance.nil?
@distance = 5
end
这里是Shop.rb
require 'net/http'
class Shop < ActiveRecord::Base
acts_as_mappable
DATA_FILE_COLS =
[
"---",
"Site",
"Zip Search",
"Shop",
"Address",
"Phone",
"Make",
"Affiliations",
"Specialties",
"Amenities",
"Timestap",
]
FIELDS =
{
"---" => "-1",
"Site" => "-1",
"Zip Search" => "-1",
"Shop" => "name",
"Address" => "raw_address",
"Phone" => "phone_number",
"Make" => "make",
"Affiliations" => "affiliations",
"Specialties" => "specialties",
"Amenities" => "amenities",
"Timestap" => "-1"
}
def full_address
"#{address_1} #{city} #{state} #{postal_code}"
end
def valid_for_geocoding?
rtn = true
if self.full_address.nil? || self.full_address.to_s.empty?
rtn = false
end
return rtn
end
def geocode_address
geo = GeoKit::Geocoders::MultiGeocoder.geocode( full_address )
errors.add(:address, "Could not Geocode address") if !geo.success
self.lat, self.lng = geo.lat,geo.lng if geo.success
end
def geocode_raw_address
geo = GeoKit::Geocoders::MultiGeocoder.geocode(self.raw_address)
if ( geo.success )
self.address_1 = geo.street_address
self.city = geo.city
self.state = geo.state
self.postal_code = geo.zip
self.lat = geo.lat
self.lng = geo.lng
end
end
def self.import( file_path )
url = 'http://example.com'
#file = '/filename.txt'
file = file_path
response = Net::HTTP.get_response(URI.parse(url + file))
body = response.body
lines = body.split("\r\n")
line_cnt = 0
lines.each { |line|
if line_cnt > 1
words = line.split("\"")
cnt = 0
shop_atrbs = Hash.new
words.each { |word|
if word != ","
#puts "#{fields[cnt]} : #{word.strip}"
field = FIELDS[DATA_FILE_COLS[cnt]]
if field != "-1"
shop_atrbs[field] = word.strip
end
cnt = cnt + 1
end
}
shop = Shop.new
shop.update_attributes(shop_atrbs)
geo = GeoKit::Geocoders::MultiGeocoder.geocode(shop.raw_address)
shop.address_1 = geo.street_address
shop.city = geo.city
shop.state = geo.state
shop.postal_code = geo.zip
shop.lat = geo.lat
shop.lng = geo.lng
shop.save
end
line_cnt = line_cnt + 1
} #f.each{ |line| puts '#{f.lineno}: #{line}' }
end
def self.fetch_not_geocoded
find( :all,
:conditions => ['lat IS NULL || lng IS NULL || lat = ? || lng = ?', "", ""],
:limit => 10000)
end
def self.find_for_sitemap(offset, limit)
find( :all,
:select => 'id, name, updated_at',
:order => 'updated_at DESC',
:offset => offset,
:limit => limit )
end
end
所以,我想要的工作是 - /Alaska/Anchorage/Shops-List/(干净的 URL)
我现在能做的是 - /find_shops?zip=anchorage&distance=5
最后,这是 rails 2.3.2,我花了很多时间试图将其转换为 rails 3.0,但还没有成功。