Tag: 路由

Rails中的模块路由with form_for(@object)

我有命名空间Controller Entities :: Customers class Entities::CustomersController < ApplicationController … end 和命名空间的ActiveRecord模型: class Entities::Customer < Entities::User end 在我的routes.rb文件中我有: resources :customers, module: :entities 模块:实体在那里,因为我不想拥有如下路线: / entities / customers但仅限于: /客户 。 问题在我渲染表单时开始: 抛出错误:类的未定义方法`entities_customer_path’.. 所以错误是rails认为正确的路径是前缀实体。 耙路给我: Prefix Verb URI Pattern Controller#Action customers GET /customers(.:format) entities/customers#index POST /customers(.:format) entities/customers#create new_customer GET /customers/new(.:format) entities/customers#new edit_customer GET /customers/:id/edit(.:format) entities/customers#edit customer GET /customers/:id(.:format) […]

Rails 3使用可选范围下的资源进行路由

我已经设置了这样的版本化API ,只需稍微调整即可实现向后兼容。 在我的路线中,我有: scope ‘(api(/:version))’, :module => :api, :version => /v\d+?/ do … scope ‘(categories/:category_id)’, :category_id => /\d+/ do … resources :sounds … end end 已达到成功的目标,让以下url到达同一个地方 /api/v1/categories/1/sounds/2 /api/categories/1/sounds/2 /categories/1/sounds/2 /sounds/2 我的目录结构是这样的: 我看到的问题在于我的观点中的链接世代。 例如,在声音节目页面上,我有一个button_to来删除声音 ‘Are you sure?’, :method => :delete %> 这会在表单action生成以下URL: “/api/sounds/1371?version=1371″ 此外, delete方法不起作用,而是作为POST发送 rake routes的有趣部分是: sounds GET (/api(/:version))(/categories/:category_id)/sounds(.:format) {:controller=>”api/sounds”, :version=>/v\d+?/, :action=>”index”, :category_id=>/\d+/} POST (/api(/:version))(/categories/:category_id)/sounds(.:format) […]

Rails 3.0.10,客户路线,post和form_for标签

我的路线中有这个: resources :events do collection do post ‘moderate’ end end 耙路线告诉我: moderate_events POST /events/moderate(.:format) {:controller=>”events”, :action=>”moderate”} 我有一个“管理”控制器,它只列出需要调节的事件: @modevents = Event.where(‘moderated 1’) 到目前为止,所有尚未审核的事件都可以在视图中显示: Display Stuff here 我想在循环中放置一个更新了审核值的表单,但对于我的生活,我无法弄清楚要放在form_for中的内容 – 我尝试过: moderate_events_path do |f| %> 返回的html是: 当我单击“提交”按钮时,我收到以下错误: Couldn’t find Event with ID=moderate 解决方案非常简单,在路线中将“post”更改为“put”: resources :events do collection do put ‘moderate’ end end 现在它可以正常工作。 更新,甚至自定义更新都是“放置”function。

当用户名中有@符号时,Rails,route_path会中断

每当用户名中包含@符号时,我会收到RoutingError: No route matches {:controller=>”users”, :action=>”show”, :username=>”abc@shin.com”} 我的路线看起来像这样: match ‘users/:username’ => ‘users#show’, :as => :show_other_user 我的观点是: user.username) %> 如果用户名没有@符号,一切正常。

如何在Rails 4中访问模型中的polymorphic_path?

非常简单,我想在Rails 4模型中使用polymorphic_path方法。 是的,我知道关注点很差。 我知道Rails.application.routes.url_helpers ,但是polymorphic_path不在那里。

在Rails中处理多个根路径和范围

我们有以下路线设置: MyApp::Application.routes.draw do scope “/:locale” do …other routes root :to => ‘home#index’ end root :to => ‘application#detect_language’ end 这给了我们这个: root /:locale(.:format) home#index root / application#detect_language 这很好。 但是,当我们想要使用语言环境生成路由时,我们遇到了麻烦: root_path生成/哪个是正确的。 root_path(:locale => :en)生成/?locale=en这是不合需要的 – 我们想要/en 所以,问题是,这是可能的,怎么样?

一个控制器用于多个路由

我一直在寻找一段时间,但我似乎无法弄清楚这是否可能。 我需要的是两个不同路径的一个控制器。 我所拥有的是一个模型,有两种类型:拥有和兼容。 所以我想要的是这样的两条路径,同时转到一个控制器: example.com/hotels example.com/compatitives 这些必须是资源,并且在这些路线中将会有很多嵌套。 所以我不想为它们创建资源映射。 我已经试过了: resources :hotels, :compatitives, :controller => :hotels do resources :rooms do collection do match “/search”, :action => :search end end collection do match “/search” match “/results/:type/:id(/:page)”, :action => :results end end resources :prices do collection do match “/check” end end 但控制器不是两者的hotels_controller。 这有可能吗? 谢谢!

奇怪的Rails路由行为:在嵌套资源中交换两个ID

我在我的应用程序中设置了以下路由(表单属于某个站点): map.resources :sites do |site| site.resources :forms end 但是,当我尝试使用帮助程序(例如,使用帮助程序)进入编辑(或类似)的路径时 edit_site_form_path(form) 要么 我的url是ID交换过的(/ sites / 5 / forms / 1),其中5是表单ID,1是网站ID。 这是来自page / sites / 1。 救命(?)

Rails Routes – 斜杠字符与哈希字符

在URL和rails路由中,使用斜杠字符与井号(井号)字符有什么区别? 这些工作 get “/static_pages/about” get ‘about’, to: ‘static_pages#about’, as: :about 这些没有 get “/static_pages#about” get ‘about’, to: ‘static_pages/about’, as: :about get ‘about’, to: ‘/static_pages#about’, as: :about 什么代码控制这种行为,背后的深层原因是什么? 回答: (这两个人回答得非常好,我很难选择哪一个标记为接受的答案。我希望以一种可能有助于人们的不同方式表明我对答案的理解。) 使用/符号后,字符串将被识别为附加到基本URL的URL字符串。 所以’#’字符将被解释为url的一部分,而url不喜欢使用’#’字符。 在不使用/字符的情况下,第一个单词以某种方式被识别为控制器名称,您可以使用“#”和操作名称进行跟进。

使用form_for(部分)路由范围问题

试图路线: scope :shortcut do resources :text_elems end 使用forms偏的基本脚手架 *_form.html.erb* @shortcut) do |f| %> … 问题是 :当我调用编辑操作时,表单html显示为: 注意: 新操作正确呈现表单操作: 因此,当form_for处理它的块时,我的:快捷方式param似乎被:id param打败了。 现在我能够使用:快捷方式参数正确路由,如果我在form_for块中手动创建:url => {…},但我希望保持代码干,再加上我要报告如果这确实是一个错误,这个问题到rails。 任何人都可以确认这是一个错误吗?