0

我遇到的问题与 Rails 3 项目的简单私人消息传递插件有关。

我的网站是一个市场,例如 GumTree.com,人们可以在其中发布他们的需求,其他人可以通过单击“联系”按钮并填写表格来做出回应,然后将表格提交给发布者。

我遇到的问题是消息表单未加载,当我转到 /messages/new 时,出现此错误:

undefined local variable or method `is_private_message' for #<Class:0x12cf23578>

代码如下 - 感谢任何反馈!

谢谢,

费萨尔


用户模型

class User < ActiveRecord::Base

has_many :posts  
has_one :profile
has_private_messages

attr_accessible :email

validates_presence_of :email
validates_uniqueness_of :email, :message =>"Hmm, that email's already taken"
validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i, :message => "Hi! Please use a valid email"

end

消息模型

class Message < ActiveRecord::Base

is_private_message

attr_accessor :to        

end

ROUTES.RB(仅相关部分)

get "messages/new"

消息> 新视图

<% form_for @message, :url => user_messages_path(@user) do |f| %>
<p>
To:<br />
    <%= f.text_field :to %>
    <%= error_message_on @message, :to %>
</p>
<p>
Subject:<br />
<%= f.text_field :subject %>
<%= error_message_on @message, :subject %>
</p>
<p>
  Message<br />
  <%= f.text_area :body %>
        <%= error_message_on @message, :body %>
</p>
<p>
  <%= submit_tag "Send" %>
</p>
<% 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.login
    @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_login(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.find(params[:user_id])
end 
end
4

1 回答 1

0

根据文档https://github.com/jongilbraith/simple-private-messages没有is_private_message方法。但是has_private_messages,您已经在 User 模型中添加了一种方法。

我认为你只需要删除is_private_message

编辑

你执行了这个命令吗?rails generate simple_private_messages:model User Message或类似的东西。根据gem的代码(https://github.com/jongilbraith/simple-private-messages/blob/master/lib/generators/simple_private_messages/model/model_generator.rb)理论上,你应该有一个迁移生成,所以rake db:migrate不应为空 。

于 2012-04-18T09:51:01.830 回答