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)))'
末
當你去消息視圖,你要去哪個URL? – 2010-11-29 22:14:19
本地主機/消息 – 2010-11-30 02:47:03