Rails识别未知的url参数

时间:2014-05-16 22:19:08

标签: ruby-on-rails routes url-parameters

我不确定为什么但最近我尝试改变我的路线以添加新项目

/ project / new,而不是呈现new.html.haml,它呈现show.html.haml,new是参数。

在我的routes.rb里面

Mika::Application.routes.draw do
  root "pages#home"
  get '/contact' => "pages#contact"
  get '/about' => 'pages#about'
  get '/privacy' => 'pages#privacy'
  get '/tos' => 'pages#terms_of_service'

  post '/contact' => 'messages#contact'

  resources :projects, :only => [:index, :show]
  resources :projects, :only => [:update, :edit, :create, :post, :new], :constraints => { :subdomain => 'admin' }
end

我不完全确定在哪里寻找纠正这个问题甚至何时开始

在终端我得到了这个

Started GET "/projects/new" for 127.0.0.1 at 2014-05-16 15:23:20 -0700
Processing by ProjectsController#show as HTML
  Parameters: {"id"=>"new"}
  Rendered projects/show.html.haml within layouts/application (0.1ms)
  Rendered layouts/_shim.html.haml (0.8ms)
  Rendered layouts/_header.html.haml (0.5ms)
  Rendered layouts/_footer.html.haml (0.2ms)
  Rendered layouts/_javascript.html.haml (0.0ms)
  Rendered layouts/_analytics.html.haml (0.0ms)
Completed 200 OK in 9ms (Views: 8.0ms | ActiveRecord: 0.0ms)

*编辑将路线文件更改为此作品

resources :projects, :only => [:update, :edit, :create, :post, :new], :constraints => { :subdomain => 'admin' }
resources :projects, :only => [:index, :show]

但它仍然为结果

的空白页面不存在的参数进行渲染

enter image description here

1 个答案:

答案 0 :(得分:0)

您已为show和new定义了这些网址:

project       GET /projects/:id(.:format)  projects#show
new_project   GET /projects/new(.:format)  projects#new {:subdomain=>"admin"}

因此,如果您不使用admin子域并输入url / projects / new路由器,则将其与更通用的项目/ show匹配。正在使用url format / projects /:id,其中string new被解释为id。

Processing by ProjectsController#show as HTML
Parameters: {"id"=>"new"}

因此,如果您想要使用和不使用admin子域访问项目/ new,请在路由中指定:

resources :projects, :only => [:index, :show, :new]
resources :projects, :only => [:update, :edit, :create, :post, :new], :constraints => { :subdomain => 'admin' }
相关问题