私はRailsでのプログラミングや一般的なプログラミングは初めてなので、ご容赦ください。モデル、ビュー、コントローラーがどのように機能するかについての理解は非常に限られていますが、できる限り早く学習しています。
既に持っているデータに基づいて半静的な中間ページを生成する方法に問題があります。現時点では、郵便番号検索、結果リストのページ、ショップ ページのみです。State-city-list-shop で構成してほしい
私のデータベースには、地理コード化され、郵便番号検索を介してクライアント側でアクセスできるようになっているショップの場所があります。そのため、通常の足場タイプのセットアップを持つ Shops コントローラーを扱っているため、ショービューの URL は /shops/1 のようになります。私が欲しいのは、ショップ/アラスカ/アンカレッジ/パンくずリストのある店の名前です。
「States」または「Cities」コントローラーはありません。これは、ルートを構築するためにそれを行う必要があると私が想定する方法です。
静的な State テンプレート ページを作成し、それぞれの State に対して、Cityのようなリンクを使用して都市をリストします。State フォルダーのそれぞれに index.html ファイルがありますが、/shop/state/city には何もありません。
これから結果を得ることができるので、私はそのようにしました:
/shops/find_shops?=zip=City&Distance=5 /state/city/shops
で同じ結果が得られたら嬉しいです。
リスト内のショップの 1 つをクリックすると、/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 に変換するために多くの時間を費やしましたが、まだ成功していません。