2012-04-22 60 views
0

当我尝试在我的Rails 3项目中提交表单时出现以下错误。当1个用户使用Simple Private Messaging插件向另一个用户发送消息时,将使用该表单。Rails 3项目中未定义的方法`find_by_login'

我现在设置网站的方式,用户不必注册或登录以发送消息。

THE ERROR我正在

NoMethodError in MessagesController#create 

undefined method `find_by_login' for #<Class:0x12a374238> 

app/controllers/messages_controller.rb:33:in `create' 

MESSAGES CONTROLLER

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 

MESSAGES> NEW VIEW(这是正在创建形式,其中)

<%= form_for @message, :url => messages_path(:user_id => @user) do |f| %> 
<br> 
<br /> 
<br /> 
<div class="field"> 
Hello! My name is <%= f.text_field :subject %> and I'm contacting you in response to your ad. I'm interested in learning more so get in touch! Here's my contact details: <%= f.text_field :body %>. 
</div> 
<button type="submit" class="btn span6 large">Submit</button> 
<% 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 

用户迁移

class CreateUsers < ActiveRecord::Migration 
def change 
create_table :users do |t| 
    t.string :email 
    t.string :salt 
    t.timestamps 
end 
end 
end 

钢轨控制台输出在寻找用户可以通过登录

>> u = User.first 
User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1 
=> nil 
>> u.login 
NoMethodError: undefined method `login' for nil:NilClass 
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.1/lib/active_support/whiny_nil.rb:48:in `method_missing' 
from (irb):2 
>> 

的routes.rb

Mysalary::Application.routes.draw do 

resources :users do 
resources :messages 
    end 

resources :profiles 
resources :pages 
resources :posts 

get "pages/home" 
get "pages/about" 
get "pages/legal" 
get "pages/feedback" 

root :to => 'posts#new' 

end 
+0

更换有问题的行是'login'您的用户模型的表? – apneadiving 2012-04-22 16:10:13

+0

@apneadiving不,它不是。见上面添加的用户迁移。我需要添加它吗?如果是的话那有什么解决办法? – hikmatyar 2012-04-22 16:14:20

回答

7

find_by_attributefind_by_login你的情况)是Rails的补充,如果你有一个在你的数据库表称为login柱(The users表你的情况)动态方法。由于您的users表不包含login列,因此User型号不具有login属性,也没有find_by_login方法。

既然你的email列尝试用这种

@message.recipient = User.find_by_email(params[:message][:to]) 
1

如果您在控制台尝试:

u = User.first 
u.login 

您能得到什么?它看起来像你的用户模型中没有登录属性,你有一个电子邮件属性。所以,您需要改用find_by_email

+0

参见上面附上的控制台输出。如果我尝试切换到'find_by_email',我会在消息控制器中得到一个“未定义的方法'user_messages_path'”错误。 – hikmatyar 2012-04-22 16:23:23

+0

看起来你试图在不定义'@ user'的情况下调用'user_messages_path(@user)'。另外,如果您的数据库中没有用户(如您的控制台示例所示),则任何查找都会返回nil,这意味着您必须在其余代码中捕获nils。无论如何,如果您稍后添加可能导致0结果的条件,最好赶上nils。 – Andrew 2012-04-22 16:28:29

+0

实际上 - '未定义的方法user_messages_path'意味着你的路由设置不正确。您的路由必须至少包含'资源:用户执行资源:消息结束'以拥有'user_messages_path'方法。 – Andrew 2012-04-22 16:30:06

相关问题