在通道视图上嵌套消息
我试图在该频道索引页面上显示属于每个频道的所有消息,有没有一种简单的方法可以做到这一点?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设有两个模型 Channel 和 Message,其中一个通道有许多消息,您可以执行以下操作:
其中
content
是Message
的某些属性Assuming two models Channel and Message, where a channel has many messages, you could do something like this:
where
content
is some attribute ofMessage