第一篇SO帖子,但我读了很多。我刚开始接触Rails,并且在学习Hartl的RailsTutorial后建立了第一个网站。
我的问题是使用STI布线。我相信路由设置正确,但子类Kid找不到“显示”路由。
使用STI的类继承
类用户<ActiveRecord::基本
班孩子<使用者
儿童管理员
def显示
@kid=kid.find(params[:id])
终止
用户控制器创建
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to kidtunes!"
if (@user.type = "Kid")
***redirect_to @kid***
else
redirect_to @parent
end
else
render 'new'
end
路线.rb
资源:用户,:孩子,:家长
root用户:'static_pages#home'
将'/help'匹配到:'static_pages#help'
匹配'/contact',到:'static_pages#contact'
匹配'/signup',到:'users#new'
结果:
kids_new GET /kids/new(.:format) kids#new
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
kids GET /kids(.:format) kids#index
POST /kids(.:format) kids#create
new_kid GET /kids/new(.:format) kids#new
edit_kid GET /kids/:id/edit(.:format) kids#edit
kid GET /kids/:id(.:format) kids#show
PUT /kids/:id(.:format) kids#update
DELETE /kids/:id(.:format) kids#destroy
parents GET /parents(.:format) parents#index
POST /parents(.:format) parents#create
new_parent GET /parents/new(.:format) parents#new
edit_parent GET /parents/:id/edit(.:format) parents#edit
parent GET /parents/:id(.:format) parents#show
PUT /parents/:id(.:format) parents#update
DELETE /parents/:id(.:format) parents#destroy
root / static_pages#home
help /help(.:format) static_pages#help
contact /contact(.:format) static_pages#contact
signup /signup(.:format) users#new
错误
我在redirect_to@kid上得到以下内容
ActionController::ActionControllerError(无法重定向到nil!):
app/controllers/users_controller.rb:16:在“创建”中
我觉得我已经检查了所有我能检查的东西,但我仍然缺少一些东西@kid应该正确地重定向到kids#show路线。我不确定我是否有一个精心设计的单表继承或基本的路由问题。
提前谢谢。
-约翰
类型
这个表单在users/new.html.erb中使用,它创建了User。
<div class="row">
<div class="span5 offset2">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :fname, "First Name" %>
<%= f.text_field :fname %>
<%= f.label :lname, "Last Name" %>
<%= f.text_field :lname %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :type, "Are you a Kid or Parent?" %>
<%= f.select :type, [['Kid','Kid'],['Parent','Parent']] %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>