我与Rails 3项目的Simple Private Messaging插件有关的问题。
我的网站是一个像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"
邮件&gt;新视图
<% 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
答案 0 :(得分:0)
根据文档https://github.com/jongilbraith/simple-private-messages,没有is_private_message
方法。但是,您已经在User模型中添加了has_private_messages
方法。
我认为您只需删除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
不应为空。