将用户转到用户消息

时间:2014-09-09 19:45:33

标签: ruby-on-rails messages

我对rails非常陌生,所以请在回复中详细说明。我正在构建一个使用设计进行身份验证的Web应用程序。我现在坚持的部分是用户到用户消息传递系统。想法是用户A登录到应用程序并可以访问用户B的个人资料,并且用户B的个人资料可以点击允许用户A向用户B撰写消息的链接。然后用户B可以登录应用程序并访问收件箱,其中将找到用户A的消息。

我相信我在这里定义发件人和收件人角色时遇到了问题,现在我正试图显示用户将撰写邮件的表单。有人可以看到我的内容吗?在这做错了?我收到以下错误。我已经读过要做的事情是将User_id字段添加到表中,但我希望使用sender_id和recipient_id来链接这些消息,这两者都等于user_id(例如,用户1 [发送者]发送一个消息给用户2 [收件人]):

未知属性:user_id

def new   @message = current_user.messages.new recipient_id:params [:sender_id] 端

此外,对于铁路专家或任何与此类似的人,您能否告知我是否朝着正确的方向前进,或提供任何指导?我在这里盲目编码,只是在我继续进行时试图弥补。任何指导都会非常感激,为我节省了很多时间。代码如下:

用户迁移

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      t.string :first_name
      t.string :last_name
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      t.datetime :remember_created_at

      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      t.timestamps
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true

  end
end

消息迁移

class CreateMessages < ActiveRecord::Migration
  def change
    create_table :messages do |t|
      t.string :content
      t.integer :sender_id
      t.integer :recipient_id
      t.timestamps
    end
  end
end

schema.rb

ActiveRecord::Schema.define(version: 20140909174718) do

  create_table "messages", force: true do |t|
    t.string   "content"
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email",                     default: "", null: false
    t.string   "encrypted_password",        default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",             default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "current_industry"
    t.integer  "years_in_current_industry"
    t.string   "hobbies"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

的routes.rb

Catalyst::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, only: [:index, :show, :destroy]

  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

users_controller

class UsersController < ApplicationController
  before_filter :authenticate_user!
    def index
      @users = User.all
    end

    def show
      @user = User.find(params[:id])
    end

    def new
    end

    def create
    end

    def edit
    end

    def update
      @user = User.find(params[:id])
      @user.update!(user_params)
      redirect_to @user
    end

    def destroy
    end

    def edit_profile
      @user = User.find(params[:id])
    end

    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_industry, :years_in_current_industry, :hobbies)
    end

    def sender
      @user = User.find(params[:id])
    end

    def recipient
      @user = User.find(params[:id])
    end

  end

messages_controller

class MessagesController < ApplicationController
  before_action :set_recipient

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

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

  private

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

user.rb

class User < ActiveRecord::Base
  has_many :from_messages, class_name: 'Message', :foreign_key => "sender_id"
  has_many :to_messages, class_name: 'Message', :foreign_key => "recipient_id"

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :current_industry, :years_in_current_industry, :hobbies

end

message.rb

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: "User"
  belongs_to :recipient, class_name: "User"

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

消息/ index.html.erb

<h2>Inbox</h2>

消息/ new.html.erb

<h1>Create Message</h1>

<%= form_for [@recipient, @message] do |f| %>

    <%= f.hidden_field :recipient_id, value: @recipient.id %>

    <%= f.label "Enter your message below" %><br />
    <%= f.text_area :content %>

    <%= f.submit "Send" %>
<% end %>

rake routes

user_messages POST   /users/:user_id/messages(.:format)     messages#create
    new_user_message GET    /users/:user_id/messages/new(.:format) messages#new
               users GET    /users(.:format)                       users#index
                     POST   /users(.:format)                       users#create
            new_user GET    /users/new(.:format)                   users#new
           edit_user GET    /users/:id/edit(.:format)              users#edit
                user GET    /users/:id(.:format)                   users#show
                     PATCH  /users/:id(.:format)                   users#update
                     PUT    /users/:id(.:format)                   users#update
                     DELETE /users/:id(.:format)                   users#destroy
            messages GET    /messages(.:format)                    messages#index
             message GET    /messages/:id(.:format)                messages#show
                     DELETE /messages/:id(.:format)                messages#destroy

2 个答案:

答案 0 :(得分:5)

<强>模型

#app/models/user.rb
class User < ActiveRecord::Base
   has_many :messages, class_name: "Message", foreign_key: "recipient_id"
   has_many :sent_messages, class_name: "Message", foreign_key: "sender_id"
end

#app/models/message.rb
class Message < ActiveRecord::Base
   belongs_to :recipient, class_name: "User", foreign_key: "recipient_id"
   belongs_to :sender, class_name: "User", foreign_key: "sender_id"
   scope :unread, -> { where read: false }
end

这使您能够创建“属于”用户(IE收件人)的邮件,然后您可以将“发件人”配置文件与这些邮件相关联。

-

<强>控制器

这将使您能够调用以下内容:

#app/controllers/messages_controller.rb
class MessagesController < ApplicationController
   before_action :set_recipient, only: [:new, :create]

   def new
      @message = current_user.sent_messages.new
   end

   def create
      @message = current_user.sent_messages.new message_params
      @message.recipient_id = @recipient.id
      @message.save
   end

   def index
      @messages = current_user.messages
   end

   def destroy
      @message = current_user.messages.destroy params[:id]
   end

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

   private

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

   def set_recipient
       @recipient = User.find params[:user_id]
   end
end

-

<强>路线

#config/routes.rb
devise_for :users, path: "", controllers: { :registrations => "registrations" }, path_names: {sign_up: "register", sign_in: "login", sign_out: "logout"}

resources :users do
   get :profile
   resources :messages, only: [:new, :create] #-> domain.com/users/:user_id/messages/new
end
resources :messages, only: [:index, :show, :destroy] #-> domain.com/messages/:id

-

<强>视图

这将使您能够使用以下链接:

#app/views/users/show.html.erb (user to send message to)
<%= link_to "Send Message", user_messages_path(@user.id) %>

#app/views/messages/new.html.erb
<%= form_for [@recipient, @user] do |f| %>
     <%= f.text_field :content %>
     <%= f.submit %>
<% end %>

#app/views/messages/index.html.erb
<h2>Inbox</h2>
<% @messages.each do |message| %>
   <%= message.content %>
<% end %>

-

<强>修正

  

我已经读过,要做的是将User_id字段添加到表中,   但我希望使用sender_id和来链接这些消息   recipient_id,两者都等于user_id(例如,用户1 [发送者]发送一个   消息给用户2 [收件人])

需要将user_id添加到您的表格中。 user_id仅仅是foreign_key,您已在models中覆盖了该文件。

您需要做的只是设置recipient_idsender_id,我们正在create方法中执行此操作:

def create
   @message = current_user.message.new message_params
   @message.recipient_id = @recipient.id
   @message.save
end

你在这里做了一些非常聪明的事情。

首先,您通过调用sender_id隐式设置current_user.messages外键。如果你打电话给Message.new,那将是一个完全不同的故事(必须设置sender_id

其次,因为您正在使用嵌套路线,所以您可以使用@recipient方法中设置的before_action变量为我们提供id recipient_id

这对你有用。除非您尝试访问子/嵌套模型中的“父”模型数据,否则不需要使用inverse_of


<强>推荐

您正在做的事情是完全有效的

核心诀窍是确保您的Message模型完全独立。独立于您的User。这是通过您的设置实现的,允许您创建所需的各种对象。

您需要考虑的另一个方面是如何确保您能够为用户提供"threaded" messages的能力。您可以使用其中一个层次结构宝石(AncestryClosure_Tree

来实现此目的

添加此功能将更加深入。如果您需要,我可以提供信息(只需发表评论)


<强>线程

层次结构宝石实际上相对简单易用。

“踩踏”消息的诀窍是使用其中一个宝石(AncestryClosure_Tree),因为它们为您提供了可以调用项目的“方法”。它们通过在数据库中创建多个列来工作,在保存/创建所需对象时填充它们

“线程”问题很重要,因为没有“层次结构”宝石,您将无法调用所需记录的“子”对象,从而防止线程发生。这是关于如何实现它的good Railscast

enter image description here

这样做的诀窍是使用名为"recursion"

的东西

递归是您创建“无限期”循环的地方,就数据的“递归”而言。 EG如果你有一个有孩子的对象,你必须循环遍历孩子,然后是那些孩子的孩子,递归直到你到达显示所有数据的位置:

  

递归是以自相似方式重复项目的过程。对于   例如,当两个镜子的表面完全平行时   相互之间,出现的嵌套图像是无限的形式   递归。

因此,您就是这样做的:

  
      
  1. 确保使用正确的父母保存对象
  2.   
  3. 要显示“线程”对话,请循环显示这些父母
  4.   
  5. 使用递归循环子女
  6.   

我们使用ancestry gem,它存储层次结构与我们发现的closure_tree gem略有不同(打算尽快使用闭包树gem)。

因此,您首先必须自己保存任何层次结构:

enter image description here

这将允许您保存该对象的各种“父母”。这意味着当您加载对象并希望循环其后代时,您将能够使用Ancestry object methods

enter image description here

这意味着您将能够使用以下内容:

#app/views/comments/index.html.erb
<%= render partial: "comments", locals: { collection: @comments } %>

#app/comments/_comments.html.erb
<% collection.arrange.each do |comment, sub_item| %>
    <%= link_to comment.title, comment_path(comment) %>

    <% if category.has_children? %>
        <%= render partial: "category", locals: { collection: category.children } %>
    <% end %>
<% end %>

答案 1 :(得分:0)

要解决您的错误,请尝试在模型类中设置:inverse_ofhas_many语句的belongs_to属性。您最终可能会有两个has_many - 每个belongs_to反向一个:

user.rb:
has_many :from_messages, :class_name => 'Message', :foreign_key => "sender_id",     :inverse_of => :sender
has_many :to_messages, :class_name => 'Message', :foreign_key => "to_id", :inverse_of => :recipient

message.rb:
belongs_to :sender, :class_name => 'User', :inverse_of => :from_messages
belongs_to :recipient, :class_name => 'User',:inverse_of => :to_messages

总的来说,我认为您的方法是消息传递系统的良好起点。您可以尝试将代码发布到https://codereview.stackexchange.com/进行详细审核。