Ruby on Rails:搜索结果的定制路线

起伏

我正在构建一个供用户提交“冒险”的应用,并且我希望设置单独的页面来按城市显示冒险。我遵循了这一建议(Ruby on Rails 4:在“搜索结果”页面上显示搜索结果”),以使搜索结果显示在单独的页面上,并且效果很好,但我想进一步介绍一下,并预先设置链接以路由用户特定城市的冒险。我不确定如何从中http://localhost:3000/adventures/search?utf8=%E2%9C%93&search=Tokyo显示结果http://localhost:3000/pages/tokyo另外,我对Rails还是很陌生。这是我的第一个项目。

routes.rb

  root 'adventures#index'
  resources :adventures do
    collection do
      get :search
    end
  end

adventures_controller

  def search
    if params[:search]
      @adventures = Adventure.search(params[:search]).order("created_at DESC")
    else
      @adventures = Adventure.all.order("created_at DESC")
    end
  end
帕万

建立一个自定义路由pages就像是

get "/pages/:city", to: "pages#display_city", as: "display_city"

并使用 params[:search]

def search
  if params[:search]
    #this won't be needed here
    #@adventures = Adventure.search(params[:search]).order("created_at DESC")
    redirect_to display_city_path(params[:search])
  else
    @adventures = Adventure.all.order("created_at DESC")
  end
end

拥有controller#actionview为相应的路线。

#pages_controller

def display_city
  @adventures = Adventure.search(params[:city]).order("created_at DESC")
  ....
  #write the required code
end

app/views/pages/display_city.html.erb
  #your code to display the city

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章