在Rails 3项目中找不到没有ID - 表单错误的用户 [英] Can't find user without ID - form error in Rails 3 project

查看:102
本文介绍了在Rails 3项目中找不到没有ID - 表单错误的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像Gumtree.com这样的网站上工作,用户在那里创建帖子,突出了他们的各种需求(例如,我需要一个在第X天的摄影师)。 有Messaging功能出现一些问题。我正在使用Simple Private Messaging插件。



问题的形式是用户填写以发送消息/回复帖子。当我尝试访问/ messages / new时遇到以下错误:

  MessagesController中的ActiveRecord :: RecordNotFound#new 
无法找到没有ID
的用户

在下面附上我的模型 - 感谢您的任何建议! p>

谢谢,

费萨尔

MESSAGES CONTROLLER

  class MessagesController< ApplicationController 

before_filter:set_user
$ b $ 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)
结束

def new
@message = Message.new

if params [:reply_to]
@reply_to = @ user.received_messages.find(params [:reply_to])
除非@ reply_to.nil?
@ message.to = @ reply_to.sender.login
@ message.subject =Re:#{@reply_to.subject}
@ message.body =\\\
\ n * Original message * \\\
\\\
#{@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] =发送消息
redirect_to user_messages_path(@user)
else
render:action => :新的
结束
结束

def delete_selected
如果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)除非@ message.nil?
}
flash [:notice] =消息已删除
结束
redirect_to:返回
结束
结束

私有
def set_user
@user = User.find(params [:user_id])
end
end

消息模型

  class消息< ActiveRecord :: Base 

is_private_message

attr_accessor:to

end

用户模型

  class User< ActiveRecord :: Base 
$ b has_many:posts
has_one:profile
has_private_messages
$ b attr_accessible:电子邮件

validates_presence_of:email
validates_uniqueness_of:email,:message =>嗯,这封电子邮件已经存在了
validates_format_of:email,:with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i,:message => 嗨!请使用有效的电子邮件


end

MESSAGE> NEW VIEW

 <%form_for @message,:url => user_messages_path(@user)do | f | %GT; 
< p>
收件人:< br />
<%= f.text_field:to%>
<%= error_message_on @message,:to%>
< / p>
< p>
主题:< br />
<%= f.text_field:subject%>
<%= error_message_on @message,:subject%>
< / p>
< p>
留言< br />
<%= f.text_area:body%>
<%= error_message_on @message,:body%>
< / p>
< p>
<%= submit_tag发送%>
< / p>
<%end%>


解决方案

您确定您的新邮件页面收到:user_id 在它的 params 中?即使这是真的,我怀疑这是你的意图,因为任何人都可以通过传递另一个:user_id 来假装是另一个用户。



所以你的行在底部:

$ p $ @ $ user = user.find(params [:user_id] )#我认为这是你的错误的原因 - 没有:user_id通过

好像必须be:

  @user = User.find(session [:user_id])



我想你已经听说过会议。


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.

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!

Thanks,

Faisal

MESSAGES CONTROLLER

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

MESSAGE MODEL

class Message < ActiveRecord::Base

is_private_message

attr_accessor :to

end

USER MODEL

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

MESSAGE>NEW VIEW

<% 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 %>

解决方案

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.

So your line in the bottom:

@user = User.find(params[:user_id]) # I think it's the cause of your error -- no :user_id is passed

seems like has to be:

@user = User.find(session[:user_id])

I suppose you've heard about sessions.

这篇关于在Rails 3项目中找不到没有ID - 表单错误的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆