STI路由错误

时间:2012-10-19 12:53:39

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

Newbie在研究了Hartl的Rails教程书&视频演员。

我正在使用STI模型:

class User < ActiveRecord::Base
class Kid < User
class Parent < User

用户拥有基本元素:姓名,电子邮件等

我遇到的问题是路由问题。我继续努力确定哪种模型最终在这种情况下最有效(STI或多态)。我已经开始使用STI了,如果我能确定路由问题,我想我可以使用它。

我的问题是我的编辑正在寻找用户控制器中的“更新”操作,当我希望它路由到更新时我一直在阅读很多关于STI路由的SO帖子,但我可以'似乎弄清楚为什么这不会正确路由。

Rspec测试。错误来自“click_button”

describe "with valid information" do
  let(:new_first)   { "New First" }
  let(:new_last)    { "New Last" }
  let(:new_email)   { "new@example.com" }
  before do
    fill_in "First Name",               with: new_first
    fill_in "Last Name",                with: new_last
    fill_in "Email",                    with: new_email
    select  "Kid",  from: "Are you a Kid or Parent"
    fill_in "Password",                 with: kid.password
    fill_in "Confirmation",             with: kid.password
    click_button "Save changes"
  end

Rspec错误:

     KidPages edit with valid information 
     Failure/Error: click_button "Save changes"
     AbstractController::ActionNotFound:
       The action 'update' could not be found for UsersController
     # (eval):2:in `click_button'
     # ./spec/requests/kids_pages_spec.rb:32:in `block (4 levels) in <top (required)>'

路线:

       root        /                         static_pages#home
       help        /help(.:format)           static_pages#help
    contact        /contact(.:format)        static_pages#contact
     signup        /signup(.:format)         users#new
     signin        /signin(.:format)         sessions#new
    signout DELETE /signout(.:format)        sessions#destroy
    kidshow        /kids/:id(.:format)       users#kidshow
  kidupate PUT    /kids/:id(.:format)       users#kidupdate
    kidedit        /kids/:id/edit(.:format)  users#kidedit
      users GET    /users(.:format)          users#index
            POST   /users(.:format)          users#create
   new_user GET    /users/new(.:format)      users#new
  edit_user GET    /users/:id/edit(.:format) users#edit
       user GET    /users/:id(.:format)      users#show
            PUT    /users/:id(.:format)      users#update
            DELETE /users/:id(.:format)      users#destroy
   sessions POST   /sessions(.:format)       sessions#create
new_session GET    /sessions/new(.:format)   sessions#new
    session DELETE /sessions/:id(.:format)   sessions#destroy

的routes.rb

  root to: 'static_pages#home'
  match '/help',    to: 'static_pages#help'
  match '/contact', to: 'static_pages#contact'
  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout',  to: 'sessions#destroy', via: :delete
  match 'kids/:id', to: 'users#kidshow',  :as => 'kidshow'
  match 'kids/:id', to: 'users#kidupdate', :via => 'put', :as => 'kidupdate'
  match 'kids/:id/edit', to: 'users#kidedit',  :as => 'kidedit'
  resources :users
  resources :sessions, only: [:new, :create, :destroy]

我几周来一直在努力解决这些问题和这个问题,我很感激你的帮助。

1 个答案:

答案 0 :(得分:1)

您可以更好地整理控制器以完全避免此问题。见下文

的routes.rb

resources :kids 
resources :parents

这将直接为您提供命名路径:

edit_kid_path(kid_id) 
edit_parent_path(parent_id)

kids_controller.rb

class KidsController < ApplicationController
  def update
  end
end

parents_controller.rb

class ParentsController < ApplicationController
  def update
  end    
end

如果要共享控制器行为,可以执行

class KidsController < UsersController
end

class ParentsController < UsersController
end

并将常用操作放在users_controller.rb中,并在子控制器中覆盖它们。

决定使用何种模型关系应该与构建控制器的方式无关。控制器和路由是一个需要考虑的领域。建模数据完全是单独考虑的。声明资源只是一些匹配路由的捷径。

如果您不打算使用#update等用户,则不需要声明resources :users,因为您没有使用声明提供的任何路由。