在rails应用程序上是否正确设置了私人消息功能?

时间:2013-03-11 03:54:23

标签: ruby-on-rails ruby-on-rails-3

我安装了简单的私人消息传递插件(https://github.com/jongilbraith/simple-private-messages)但是我一直在尝试使用user.name(NOT user.login)来解决这个问题。我没有收到任何错误消息,但似乎没有收到任何已发送的消息,并且没有任何内容显示在“已发送”下。

首先是用户架构

create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at",                         :null => false
    t.datetime "updated_at",                         :null => false
    t.string   "password_digest"
    t.string   "remember_token"

消息架构

create_table "messages", :force => true do |t|
    t.integer  "sender_id"
    t.integer  "recipient_id"
    t.boolean  "sender_deleted",    :default => false
    t.boolean  "recipient_deleted", :default => false
    t.string   "subject"
    t.text     "body"
    t.datetime "read_at"
    t.datetime "created_at",                           :null => false
    t.datetime "updated_at",                           :null => false
  end

消息控制器     class MessagesController< ApplicationController中

  before_filter :set_user

def index
if params[:mailbox] == "sent"
  @messages = @user.sent_messages
else
  @messages = @user.received_messages
end
end

def show
@message = Message.read_message(params[:id], current_user)
end

def new
@message = Message.new

if params[:reply_to]
  @reply_to = @user.received_messages.find(params[:reply_to])
  unless @reply_to.nil?
    @message.to = @reply_to.sender.name
    @message.subject = "Re: #{@reply_to.subject}"
    @message.body = "\n\n*Original message*\n\n #{@reply_to.body}"
  end
end
end

def create
@message = Message.new(params[:message])
@message.sender = @user
@message.recipient = User.find_by_name(params[:message][:to])

if @message.save
  flash[:notice] = "Message sent"
  redirect_to user_messages_path(@user)
else
  render :action => :new
end
end

def delete_selected
if request.post?
  if params[:delete]
    params[:delete].each { |id|
      @message = Message.find(:first, :conditions => ["messages.id = ? AND (sender_id = ? OR recipient_id = ?)", id, @user, @user])
      @message.mark_deleted(@user) unless @message.nil?
    }
    flash[:notice] = "Messages deleted"
  end
  redirect_to :back
end
end

private
def set_user
 @user = User.first
end
end

消息模型

class Message < ActiveRecord::Base
  attr_accessible :to, :subject, :body
  is_private_message

  attr_accessor :to

end

消息index.html.erb

<%= form_tag delete_selected_user_messages_path(@user, :method => :post) do %>
    <% if params[:mailbox] == "sent" %>
            <%= render :partial => "sent" %>
    <% else %>
            <%= render :partial => "inbox" %>
        <% end %>
<% end %>

消息sent.html.erb

<h2>Sent</h2>
<table>
    <tr>
        <th>Del?</th>
        <th>Subject</th>
        <th>To</th>
        <th>Sent</th>
    </tr>
    <% if @messages.size == 0 %>
        <tr>
            <td colspan="4">
                No messages
            </td>
        </tr>
    <% else %>
        <% for message in @messages %>
            <tr>
                <td><%= check_box_tag "delete[]", message.id %></td>
                <td><%= link_to h(message.subject), user_message_path(@user, message) %></td>
            <td><%= link_to h(message.recipient.login), user_path(message.recipient) %></td>
              <td><%=h message.created_at.to_s(:long) %></td>
            </tr>
        <% end %>
        <tr>
            <td colspan="4">
                <%= submit_tag "Delete" %>
            </td>
        </tr>
    <% end %>
</table>
<%= link_to "Inbox", user_messages_path(@user)%>

我做错了吗?任何有关这方面的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果你看一下控制器/节目

def show
  @message = Message.read_message(params[:id], current_user)
end

您会看到第二个参数始终是当前用户。所以它返回了当前用户的交互。