嵌套资源

发布于 2024-10-05 05:53:22 字数 2626 浏览 7 评论 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-10-12 05:53:22

这里发生的事情是,这一行:

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

正在失败,因为 params 哈希中没有定义的channel_id。我看到你正在使用浅层路由,这意味着你的uri可能看起来像这样:

/messages

并且你需要它看起来像这样:

/channels/1/messages

尝试将你的url更改为:

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

What's going on here is that this line:

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

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:

/messages

And you need it to look like:

/channels/1/messages

Try changing your url to be:

channel_messages_url(@channel)

instead of

messages_url

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:

# assuming you need shallow routes
resources :channels, :shallow => true do |channels|
  channels.resources :messages
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文