无法找到没有ID的用户-Rails 3项目中的表单错误 [英] Can't find user without ID - form error in Rails 3 project
问题描述
我正在Gumtree.com之类的网站上工作,用户可以在其中创建帖子,突出他们的各种需求(例如,我在第X天需要一名摄影师).
I am working on a website like Gumtree.com, where Users create Posts, highlighting their various needs (e.g. I need a photographer on day X).
消息传递"功能遇到了一些麻烦.我正在使用简单私人消息传递插件.
Having some trouble with the Messaging functionality. I am using the Simple Private Messaging plugin.
问题的形式是用户填写以发送消息/回复帖子.尝试访问/messages/new时出现以下错误:
Problem is in the form that Users fill out to send a message / respond to a post. I get the following error when I try to access /messages/new:
ActiveRecord::RecordNotFound in MessagesController#new
Couldn't find User without an ID
在下面安装我的模型-感谢您提供任何建议!
Attaching my models below - grateful for any advice!
谢谢
失败
消息控制器
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
消息模型
class Message < ActiveRecord::Base
is_private_message
attr_accessor :to
end
用户模型
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
消息>新视图
<% 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 %>
推荐答案
您确定新邮件页面的 params
中收到:user_id
吗?即使是真的,我也怀疑这是您的意图,因为任何人都可以通过仅传递另一个:user_id
来假装成为另一个用户.
Are you sure that your page for new messages receives :user_id
in it's params
? Even if it is true, I doubt that this is was your intention because anyone can pretend to be another user by just passing another :user_id
.
所以您的行在底部:
@user = User.find(params[:user_id]) # I think it's the cause of your error -- no :user_id is passed
好像必须这样:
@user = User.find(session[:user_id])
我想你听说过会议.
这篇关于无法找到没有ID的用户-Rails 3项目中的表单错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!