0

最初のSO投稿ですが、私はたくさん読んだことがあります。HartlのRailsTutorialを勉強して以来、Railsを初めて使用し、最初のサイトを構築しています。

私の問題は、STIを使用したルーティングです。ルートは正しく設定されていると思いますが、サブクラスのKidは「show」ルートを見つけられません。

STIを使用したクラス継承

クラスUser<ActiveRecord:: Base

クラスキッド<ユーザー

キッドコントローラー

デフショー

@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

リソース:users、:kids、:parents

ルート:'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:in `create '

確認できることはすべて確認したように感じますが、まだ何かが足りません。@kidは、kids#showルートに適切にリダイレクトする必要があります。巧妙に作成されていない単一テーブル継承があるのか​​、基本的なルーティングの問題があるのか​​わかりません。

前もって感謝します。-ジョン

このフォームはusers/new.html.erbで使用され、ユーザーを作成します。

<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 %>

4

2 に答える 2

1

私が最初に気付いたのは、あなたが次=の代わりに使用しているということです==

if (@user.type = "Kid")

私はそれをそのようにテストする方が良いと思います:

if @user.is_a? Kid

@kidどのように設定したかを教えていただけます@parentか?

于 2012-09-07T04:18:16.493 に答える
1

@kidまたは@parent変数に値を定義/割り当てましたか?そうでない場合は、になり、質問に含めnilcannot redirect to nilエラーが発生します。

アクションの完全なコードを含めてくださいcreate。そうでなければ、リダイレクトで何が起こっているのかを正確に(自分で読むのではなく)信頼することになります。

リダイレクトにもいくつかの作業が必要になる場合があります。たとえば、次のことができます。

if (@user.is_a? Kid)
  redirect_to kid_path(@user)
else
  redirect_to parent_path(@user)
end

...またはそれに非常に似たもの。

于 2012-09-07T04:19:49.060 に答える