Rails别名路由 - 无效路由

时间:2014-06-10 07:26:11

标签: ruby-on-rails ruby-on-rails-3 rails-routing

情况:对于推荐,用户可以创建单独的URL别名,这些别名保存在表格见证中。

所以我在路线文件的末尾创建了以下路线:

get '*alias', to: 'testimonial#show', as: 'testimonial_show'

这会重定向到推荐控制器,显示我在哪里获取正确的推荐

问题: 通常当我输入网址www.example.com/jobs时,它会重定向到正确的作业页面,但添加了上述(别名)行后,它会尝试重定向到推荐控制器而不是作业。

这是我的路线:

mount Ckeditor::Engine => '/ckeditor'

  # get "custom_page/testimonials"
  match '/404', :to => 'home#not_found'
  match '/422', :to => 'home#rejected'
  match '/500', :to => 'home#server_error'

  # resource :testimonial, :only => :index
  get "testimonial", to: 'testimonial#index', as: 'testimonial_index'
  get "testimonial/:id", to: 'testimonial#show', as: 'testimonial_show'
  get "video_testimonial/:id", to: 'testimonial#show_video', as: 'video_testimonial_show'
  get '/robots.txt' => 'home#robots'
  get "custom_page/evp"

  devise_for :users, :controllers => { :registrations => "registrations", :passwords => "passwords" }

  devise_scope :user do
    get "/users" => "registrations#new"
    get "/users/password", to: "passwords#create"
  end

  as :user do
    get "/login" => "home#index", :as => "new_user_session"
    delete "/logout" => "devise/sessions#destroy"
  end


  # User created pages
  resources :pages, :only => :show
  resources :jobs, :only => [:show, :index, :create] do
    resources :apply, :only => [:index, :create]
    resources :share, :only => [:index, :create]
  end

  get "/quick_sign_in" => "quick_sign_in#index"

  resources :apply, :only => :index do
    collection { get 'add'}
  end

  namespace :user do
    resource :profile, :only => [:show, :destroy, :update] do
      get "remove", :on => :member
      resources "attachments"

      collection { get 'add_attachment'}
    end
    resources :applications, :only => [:index, :destroy] do
      get "correspondence", :on => :collection
    end
    resources :jobs, :only => [:index, :create, :destroy]
  end

  root :to => 'home#index'
  get '*alias', to: 'testimonial#show', as: 'testimonial_show'

编辑1:访问作业页面,当我访问www.example.com/jobs并添加全局匹配器后,我得到以下内容:

No route matches {:controller=>"testimonial", :action=>"show", :id=>1, :locale=>:en}

编辑2:没有SQL部分的日志:

[2014-06-10 09:37:08 +0200] Started GET "/jobs" for 127.0.0.1 at 2014-06-10 09:37:08 +0200
[2014-06-10 09:37:08 +0200]   Rendered jobs/_search_sidebar.html.erb (4.3ms)
[2014-06-10 09:37:08 +0200]   Rendered jobs/_jobs.html.erb (2.3ms)
[2014-06-10 09:37:08 +0200]   Rendered jobs/_testimonial.html.erb (3.6ms)
[2014-06-10 09:37:08 +0200]   Rendered jobs/index.html.erb within layouts/application (200.6ms)
[2014-06-10 09:37:08 +0200] Completed 500 Internal Server Error in 515.2ms
[2014-06-10 09:37:08 +0200]
ActionController::RoutingError (No route matches {:controller=>"testimonial", :action=>"show", :id=>1, :locale=>:en}):
  app/views/jobs/_testimonial.html.erb:14:in `_app_views_jobs__testimonial_html_erb__481122295330206577_70303349814440'
  app/views/jobs/index.html.erb:17:in `_app_views_jobs_index_html_erb__1584069629091687413_70303340318260'

编辑3:rake routes | grep job enter image description here

2 个答案:

答案 0 :(得分:1)

您的工作页面正在尝试在第17行呈现testimonials部分,而在该部分内部(第14行)是无法存在的不存在的推荐信。

答案 1 :(得分:1)

您不能有两个名为'testimonial_show'的命名路线

您应该将第二个(全球一个)更改为其他内容......

get '*alias', to: 'testimonial#show', as: 'testimonial_glob_show'