Ruby on rails 在Rails的模板中使用来自静态URL的POST或GET请求

Ruby on rails 在Rails的模板中使用来自静态URL的POST或GET请求,ruby-on-rails,custom-routes,Ruby On Rails,Custom Routes,请容忍我,因为我对Rails和一般编程都是新手。我对模型、视图和控制器的功能了解非常有限,但我一直在尽可能快地学习 我在如何基于已有数据生成半静态中间页方面遇到了问题。现在它只是一个邮政编码搜索,一个带有结果列表的页面和一个商店页面。我希望它由州立城市列表商店组织 在我的数据库中,商店的位置已经进行了地理编码,并且可以通过邮政编码搜索在客户端访问。因此,我正在处理一个Shops控制器,它具有通常的scaffold类型设置,因此显示视图的url看起来像/Shops/1。我想要的是商店/阿拉斯加/安

请容忍我,因为我对Rails和一般编程都是新手。我对模型、视图和控制器的功能了解非常有限,但我一直在尽可能快地学习

我在如何基于已有数据生成半静态中间页方面遇到了问题。现在它只是一个邮政编码搜索,一个带有结果列表的页面和一个商店页面。我希望它由州立城市列表商店组织

在我的数据库中,商店的位置已经进行了地理编码,并且可以通过邮政编码搜索在客户端访问。因此,我正在处理一个Shops控制器,它具有通常的scaffold类型设置,因此显示视图的url看起来像/Shops/1。我想要的是商店/阿拉斯加/安克雷奇/面包屑店的名称

我没有州或城市的控制员,我认为你必须这样做才能建造路线

我制作了静态模板页面,对于每个州,我都会列出城市的链接。我在每个州文件夹中都有一个index.html文件,但在/shop/State/city中没有

我这样做是因为我可以从中得到结果:

/商店/查找商店?=zip=城市和距离=5 如果我能在/州/市/商店得到同样的结果,我会很高兴

因为这样我就可以点击列表中的一家商店,它会把我带到商店页面,它是/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
这是商店

所以,我想要的是-/Alaska/Anchorage/Shops List/clean URL

我现在能做的是- /查找店铺?邮编=锚地距离=5


最后,这是rails 2.3.2,我花了很多时间试图将其转换为rails 3.0,但还没有成功。

您是否从外到内阅读了rails布线指南?特别介绍了如何实现这一点。

前几天我刚刚看到了这一点,并试图看看它如何应用。问题是,我从来都不确定它是否是正确的解决方案,我可能已经直截了当地寻找了答案,但没有意识到。我一直在努力更好地理解路由,所以我一直在尝试不同的变体,如“匹配”和“资源做”以及=>shopsindex。我正在查看错误控制器,您可以在其中设置错误页面显示的内容。好的,也许Route Globbing就是答案*a/foo/*b=>testindex。所以*a可以是shops/state-foo可以是city-and*b可以是address/shop-name-所以你需要params[:a]表示state,params[:b]表示shop-name?Params是指我怀疑的身份?
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