Tag: 路由

自定义收集路由的polymorphic_path

我有以下路线定义: resources :documents do collection do post :filter end end 以及以下模型结构: class Document true end class User :documentable end 和控制器结构: class DocumentsController < ApplicationController def index # not important end def filter # not important end end 我可以轻松地在一个视图中说: polymorphic_path([@user, Document]) 获取路径/ users / 1 / documents ,但我希望能够说: filter_polymorphic_path([@user, Document]) 不幸的是,获取路径/ users / 1 / documents […]

在RESTful路由中获取基于“名称”的URL而不是基于id的URL

如果我为控制器“画廊”实现RESTful路由,如下所示: map.resources :galleries 默认情况下,此控制器的show url为: /galleries/:id 这会响应对/ gallery / 1等的任何请求 如果我在数据库中有一个带有’name’属性并且值为’portraits’的库记录,该怎么办? 我可以这样做: /galleries/portraits 而不是做 /galleries/1 ?

如何在Rails 3中创建动态根?

我的webapp中有管理员和普通用户。 我想根据他们的身份使他们的根(/)不同。 从许多不同的页面访问根,因此如果我能在routes.rb文件中实现这一点会容易得多。 这是我当前的文件。 ProjectManager::Application.routes.draw do root :to => “projects#index” end 有人可以把我链接到一个可以告诉我进入方向的例子吗? 有没有办法将逻辑放入路径文件? 谢谢你的帮助。

多态控制器和调用对象

我有一个多态关系,地址既可以由成员拥有,也可以由依赖者拥有。 一切都看起来很棒,直到我意识到我不知道什么类型的物体正在创造它,除非我遗漏了什么。 有没有办法告诉路由文件包含对象的类型? 楷模: class Member < ActiveRecord::Base has_one :address, as: :person, dependent: :destroy end class Dependent < ActiveRecord::Base has_one :address, as: :person, dependent: :destroy end class Address < ActiveRecord::Base belongs_to :person, polymorphic: true end 控制器: def new @person = ??? @address = Address.new(person: @person) end 目前路线: resources :members do resources :addresses, shallow: true resources […]

在轨道上测试ruby路线的位置

在轨道上测试ruby路线的位置? unit testing? function测试? 集成测试? 加成: 确切地说,在哪里应用指南和api上描述的断言?

Ruby on Rails:路由树的层次结构

所以我们有一个遗留系统跟踪ID为“欧洲/法国/巴黎”的地方,我正在构建一个Rails外观,将其转换为http:// foobar / places / Europe / France / Paris等URL。 这个要求是不可协商的,可能的级别数量无限制,而且我们无法逃避斜线。 为http:// foobar / places / Europe设置routes.rb是微不足道的: map.resources :places …但是http:// foobar / places / Europe / France抱怨“没有行动回应欧洲”。 我试过了: map.connect ‘/places/:id’, :controller => ‘places’, :action => ‘show’ …但是这给出了相同的结果,显然:id结束于第一个’/’。 如何使ID覆盖“地点”之后的任何内容和所有内容?

如何在rails中定义自定义路径?

我有一个用户模型。 如果我做: def my_action @user = User.new end 然后 我明白了 undefined method `users_path’ for # 这有意义,因为我还没有将它映射到map.resources :users. ..但我不想这样做,因为我不需要所有的资源。 如何在路由中定义此user_path方法?

Rails 4 i18n,如何转换子域用于语言环境的路由

我正在使用子域来确定Rails 4网站中的区域设置。 我有一个完全按照我想要的方式使用区域设置切换器,但现在我需要翻译路线,我不确定最好的方法。 我查看了https://github.com/kwi/i18n_routing i18n路由gem,但这不适用于子域,它似乎通过添加/ locale来改变路径,这不是我需要的。 其他gem似乎与Rails 4过时了。 编辑 基本上我希望能够使用相同的视图助手,但是更改URL以使用所选子域反映的任何语言。 这只是路线翻译。 我有语言特定的模板,我可以生成语言的导航模板,但我真的不想担心改变erb路径和url erb标签 结束编辑 来自routes.rb的示例 scope module: ‘countries’, shallow: true do get ‘south_american’, to: ‘south_america#index’, as: :south_america scope module: ‘south_america’ do get ‘south-america-weather’, to: ‘weather#index’, as: :south_america_weather get ‘south-america-gps-maps’, to: ‘gps#index’, as: :south_america_gps get ‘south-america-accommodation’, to: ‘hotels#index’, as: :south_america_hotels get ‘south-america-vacations’, to: ‘vacations#index’, as: :south_america_vacations […]

Rails:嵌套资源冲突,如何根据被调用的路由确定索引操作的范围

想象一下,你有两个定义的路线: map.resources articles map.resources categories, :has_many => :articles 都可以通过助手/路径访问 articles_path # /articles category_articles_path(1) # /category/1/articles 如果你访问/articles ,则执行ArticlesController index操作。 如果你访问/category/1/articles ,那么也会执行ArticlesController index操作。 那么,根据呼叫路由有条件地仅选择范围文章的最佳方法是什么? #if coming from the nested resource route @articles = Articles.find_by_category_id(params[:category_id]) #else @articles = Articles.all

在Rails中使用GET方法选项的button_to

我有以下按钮,我将其覆盖以生成GET请求: = button_to “Tutor”, {:controller => “appointments”, :action => “new”, :listing_id => @listing.id} , :method => :get 但是,我仍然得到一个带有额外参数的POST请求:方法: Processing by AppointmentsController#new as HTML Parameters: {“authenticity_token”=>”AWkL”, “listing_id”=>”2”, “method”=>”get”} 我的路线档案,我有: resources :appointments 我做错了什么? 谢谢。