2016-04-09 20 views
0

と私は、私もそこにcurrentplayersを行い、ネストされた二つのモデル、 Gameround>Railsの「redirect_to」ネストしたルート私のレールのアプリで最新モデル

currentplayer Gameroundがplay.html.erbに表示されています。 currentplayerが作られたとき、私はcurrentplayer#showにユーザーをリダイレクトしたいが、私はリンクをルーティングする方法を理解できないようだ。私は考えることができるすべてを試しました。 thiscurrentGameround/ThiscurrentplayerIjustmade

私のコントローラに

のGet URL:

は、だから私は言うのリンクを必要とする

def createPlayerforUser 

     @latest_game_round = Gameround.order(created_at: :desc).first 
     @currentplayer = @latest_game_round.currentplayers.create({ 
      log_id: @current_user.id 
     }); 

     if @currentplayer.save 

      redirect_to url_for([@gameround, @currentplayer]) 
     end 

    end 

config.routes

resources :gamerounds do 
    resources :currentplayers 
    end 
    resources :gamesessions 
    # The priority is based upon order of creation: first created -> highest priority. 
    # See how all your routes lay out with "rake routes". 

# Artikkel, Alien liste 
    resources :expansions do 
    resources :aliens 
    end 

    resources :users 

    # You can have the root of your site routed with "root" 
    #root 'gamesessions#play' 
    root 'gamesessions#new' 
    #root 'application#show' 
    #root 'public#index' 
    get "signup", :to => "users#new" 
    get "login", :to => "sessions#login" 
    post "login_attempt", :to => "sessions#login_attempt" 
    get "logout", :to => "sessions#logout" 
    get "profile", :to => "sessions#profile" 
    get "setting", :to => "sessions#setting" 
    get "play", :to => "gamesessions#play" 
    get "wait", :to => "gamesessions#wait" 
    get "aliens", :to => "aliens#index" 
+0

端末に '$ rake routes'と入力してください。そこにはルートの名前が表示されます。この場合、 'redirect_to gamearound_currentplayer_path(@gamearound、@currentplayer)'になります。 –

+0

答えに時間をとってくれてありがとう:)しかし、私はそれを試してきましたが、それは何らかの理由で動作しません。私は以下のコメントにエラーメッセージを掲載しました。 – Candielope

答えて

1

あなたの場合期間内にレーキルートを走らせるあなたのルートリストには、現在のプレイヤーを表示するためのものが含まれている必要があります。あなたは使用できるはずです

redirect_to gameround_currentplayer_url(@latest_game_round, @current_player) 
+0

お返事ありがとうございます!私はルートを見て、それを試してみましたが、それは私にこのエラーメッセージが与えてきました:#用GamesessionsController番号のプレーで NoMethodError 未定義のメソッド 'gameround_currentplayerを」 Candielope

+0

申し訳ありませんが、私の答えにタイプミスがあった、これ私は訂正しました。 – margo

+0

ありがとう!私はまだ現行のIDを見つけなければならなかったので、私はこれをやった。そして今、それは完全に動作します!すべてのあなたの助けをありがとう! 1 ' @currentPid = @ latest_game_round.currentplayers.find_by(LOG_ID:@ current_user.id) \t \t \t redirect_toのgameround_currentplayer_url(@latest_game_round、@currentPid)' – Candielope

関連する問題