在通道视图上嵌套消息

发布于 2024-10-04 22:41:25 字数 2668 浏览 3 评论 0原文

我试图在该频道索引页面上显示属于每个频道的所有消息,有没有一种简单的方法可以做到这一点?

    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

&

MyApp::Application.routes.draw do

  resources :users
  resources :channels , :has_many => :messages, :shallow => true
  resources :messages , :only => [:index]
  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)))'
  #root :to => 'channels#index', :as => :listchannels



end

I'm trying to show all messages that belong to each channel on that channels index pages, is there a simple way to do this?

    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

&

MyApp::Application.routes.draw do

  resources :users
  resources :channels , :has_many => :messages, :shallow => true
  resources :messages , :only => [:index]
  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)))'
  #root :to => 'channels#index', :as => :listchannels



end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

极致的悲 2024-10-11 22:41:25

假设有两个模型 Channel 和 Message,其中一个通道有许多消息,您可以执行以下操作:

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

# index.html.erb
<% @messages.each do |message| %>
  <%=h message.content %><br>
<% end %>

其中 contentMessage 的某些属性

Assuming two models Channel and Message, where a channel has many messages, you could do something like this:

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

# index.html.erb
<% @messages.each do |message| %>
  <%=h message.content %><br>
<% end %>

where content is some attribute of Message

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文