Я пытаюсь показать все сообщения, которые принадлежат каждому каналу на индексных страницах этого канала, есть ли простой способ сделать это?
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