嵌套资源
我正在尝试将消息控制器嵌套到通道控制器中。 出现错误“无法找到没有 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
Channelscontrollerroutes.rb
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
但是,当我转到消息视图时,
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)))'
end
I'm trying to nest my messages controller into my channels controller. But when I go to my messages view i get the error "couldn't find channel with no 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
Channels controller
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)))'
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里发生的事情是,这一行:
正在失败,因为 params 哈希中没有定义的channel_id。我看到你正在使用浅层路由,这意味着你的uri可能看起来像这样:
并且你需要它看起来像这样:
尝试将你的url更改为:
而不是
这是一个猜测顺便说一句,这可能是因为你的方式已经定义了
routes.rb
,这看起来有点奇怪,因为您定义了两次消息路由,一次使用 has_many,另一次作为适当的资源。你可能想要这样的东西:What's going on here is that this line:
Is falling over because there's no defined channel_id in the params hash. I see you're using shallow routes, which means your uri probably looks like this:
And you need it to look like:
Try changing your url to be:
instead of
This is a guess btw, it could be because of the way you've defined your
routes.rb
, which looks a little odd because you're defining the messages routes twice, once with the has_many and again as a proper resource. You probably want something like: