2010-11-29 260 views
0

我試圖將我的消息控制器嵌入到我的通道控制器中。但是,當我去我的郵件查看我的錯誤 「找不到沒有ID通道」嵌套資源

class MessagesController < ApplicationController 

    def index 
    @channel = Channel.find(params[:channel_id]) 
    @messages = @channel.messages 
    end 

    def new 
    @channel = Channel.find(params[:channel_id]) 
    @message = @channel.messages.build 
    end 

    def create 
    @channel = Channel.find(params[:channel_id]) 
    @message = @channel.messages.build(params[:message]) 
    if @message.save 
     flash[:notice] = "Successfully created message." 
     redirect_to channel_url(@message.channel_id) 
    else 
     render :action => 'new' 
    end 
    end 

    def edit 
    @message = Message.find(params[:id]) 
    end 

    def update 
    @message = Message.find(params[:id]) 
    if @message.update_attributes(params[:message]) 
     flash[:notice] = "Successfully updated message." 
     redirect_to channel_url(@message.channel_id) 
    else 
     render :action => 'edit' 
    end 
    end 

    def destroy 
    @message = Message.find(params[:id]) 
    @message.destroy 
    flash[:notice] = "Successfully destroyed message." 
    redirect_to channel_url(@message.channel_id) 
    end 
end 

通道控制器

class ChannelsController < ApplicationController 

    def index 
    @channels = Channel.find(:all) 
    end 

    def show 
    @channel = Channel.find(params[:id]) 
    @message = Message.new(:channel => @channel) 
    end 

    def new 
    @channel = Channel.new 
    end 

    def create 
    @channel = Channel.new(params[:channel]) 
    if @channel.save 
     flash[:notice] = "Successfully created channel." 
     redirect_to @channel 
    else 
     render :action => 'new' 
    end 
    end 

    def edit 
    @channel = Channel.find(params[:id]) 
    end 

    def update 
    @channel = Channel.find(params[:id]) 
    if @channel.update_attributes(params[:channel]) 
     flash[:notice] = "Successfully updated channel." 
     redirect_to @channel 
    else 
     render :action => 'edit' 
    end 
    end 

    def destroy 
    @channel = Channel.find(params[:id]) 
    @channel.destroy 
    flash[:notice] = "Successfully destroyed channel." 
    redirect_to channels_url 
    end 
end 

的routes.rb

SeniorProject::Application.routes.draw do 

    resources :users 
    resources :channels, :shallow => true do |channels| 
    channels.resources :messages 
    end 

    root :channels 

    resources :users, :user_sessions 

    match 'login' => 'user_sessions#new', :as => :login 
    match 'logout' => 'user_sessions#destroy', :as => :logout 
    match ':controller(/:action(/:id(.:format)))' 

+0

當你去消息視圖,你要去哪個URL? – 2010-11-29 22:14:19

+0

本地主機/消息 – 2010-11-30 02:47:03

回答

2

這裏發生了什麼事是這條線:

@channel = Channel.find(params[:channel_id]) 

由於params散列中沒有定義的channel_id,導致摔倒。我看你用淺淺的路線,這意味着你的URI可能是這樣的:

/messages 

而你需要它看起來像:

/channels/1/messages 

試着改變你的網址是:

而不是
channel_messages_url(@channel) 

messages_url 

這是一個猜測順便說一句,這可能是因爲你已經定義了你的routes.rb,這看起來有點奇怪,因爲你定義了兩次消息路由,一次是has_many,一次是適當的資源。你可能想要這樣的東西:

# assuming you need shallow routes 
resources :channels, :shallow => true do |channels| 
    channels.resources :messages 
end