新手在学习了 Hartl 的 Rails 教程书籍和视频后,正在开发他的第一个 Rails 应用程序。
我正在使用 STI 模型,其中:
class User < ActiveRecord::Base
class Kid < User
class Parent < User
用户具有基本元素:姓名、电子邮件等。
我遇到的问题是路由。我将继续努力确定哪种模型最终在这种情况下效果最好(STI 或多态)。我从 STI 开始,如果我能确定路由问题,我想我可以让它工作。
我的问题是我的编辑正在寻找用户控制器中的“更新”操作,当我希望它路由到 Kidupdate我一直在阅读许多关于 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
路线.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]
几周以来,我一直在为这些概念和这个问题苦苦挣扎,感谢您的帮助。