创建收件箱/发件箱页面

时间:2014-09-19 09:22:35

标签: ruby-on-rails messages inbox

为新手问题道歉,但我正在为我的网络应用程序正在开发一个消息传递功能,并尝试将收到的消息与已发送的消息分开。有点像收件箱/发件箱。是否可以为发送的消息和收到的消息创建单独的索引页?如果有人做过这样的事情,我真的很感激您提供的任何指导。我的代码在下面,谢谢!:

routes.rb中:

Application.routes.draw do

  devise_for :users, :controllers => { :registrations => "registrations" }

  devise_scope :user do
    get 'register', to: 'devise/registrations#new'
    get 'login',    to: 'devise/sessions#new',     as: :login
    get 'logout',   to: 'devise/sessions#destroy', as: :logout
  end

  resources :users do
    member do
     get 'edit_profile'
   end
  resources :messages, only: [:new, :create]
  end

  resources :messages

  root to: "home#index"
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get' 
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/legal',   to: 'static_pages#legal',   via: 'get'

end

messages_controller.rb

class MessagesController < ApplicationController

  def index
    @messages = current_user.to_messages
  end

  def show
    @message = current_user.to_messages.find params[:id]
  end

  def new
    @message = Message.new
    @recipient = User.find(params[:user_id])
  end

  def create
    @message = Message.new message_params
    @message.sender_id = current_user.id
    if @message.save
      flash[:success] = "Your message has been sent!"
      redirect_to users_path
    else
      flash[:failure] = "Please try again."
      redirect_to users_path
    end
  end

  def destroy
    @message = Message.find(params[:id])
    @message.destroy
    redirect_to messages_path
  end

  private

  def message_params
    params.require(:message).permit(:content, :sender_id, :recipient_id)
  end

end

messages.rb

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: 'User', foreign_key: 'recipient_id'
  belongs_to :recipient, class_name: 'User', foreign_key: 'sender_id'

  validates :content, presence: true, length: { maximum: 500 }
  validates :sender_id, presence: true
  validates :recipient_id, presence: true

  attr_accessible :sender_id, :recipient_id, :content
end

1 个答案:

答案 0 :(得分:0)

讯息

不要做出不同的&#34;索引&#34;视图 - 只需为inbox控制器创建outboxmessages视图:

#config/routes.rb
resources :users do
   resources :messages
end

resources :messages do 
     get :sent, action: "index", type: "sent", on: :collection #-> domain.com/messages/sent
end

这将使您能够创建以下内容:

#app/models/user.rb
class User < ActiveRecord::Base
   has_many :to_messages ...
   has_many :from_messages ...
end

#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
   def inbox
       type = (params[:type] && params[:type] == "sent") ? "from_messages" : "to_messages"
       @messages = current_user.send(type)
   end
end

这将使您能够访问&#34;收件箱&#34;和&#34;发件箱&#34;包含以下网址的路线:

domain.com/messages #-> messages#index
domain.com/messages/sent #-> messages#index(type=sent) 

我估计您已经对User模型进行了身份验证(只要您访问它就会对您进行身份验证的IE)。区别在于您从模型中检索的数据 - 如果您访问sent路线,则您将能够访问&#34;来自&#34;消息,如果您访问标准&#34;索引&#34;,它应该只显示&#34;收件箱&#34; to_messages


<强>协会

您尝试实现的最重要方面之一将是您从Message模型存储/访问数据的方式。

毫无疑问,您可以通过某种方式来管理消息的来自&#34;来自&#34;以及它是谁&#34;到#34;。此数据分配将决定如何访问所需的数据。

我使用以下内容:

#app/models/user.rb
class User < ActiveRecord::Base
   has_many :to_messages #-> messages with user_id" 
   has_many :from_messages #-> messages with "from_id"
end

如果您使用有关User关联的信息更新您的问题,它会为我提供更好的信息,因此我可以为您提供更有条理的回复!