我正在尝试将行为用作能够发送消息的 gem,并且我正在关注他们的示例控制器
已解决 查看答案
尝试在视图中发送消息时,我不断收到此错误undefined method `send_message' for nil:NilClass
我应该如何调整我的代码?谢谢
查看(表格)
<%= simple_form_for ActsAsMessageable::Message.new, :url => messages_path, :method => :post do |f| %>
<%= f.hidden_field :to, value: @gear.user.email %>
<%= f.input :body %>
<%= f.input :topic %>
<%= f.button :submit, class: 'btn' %>
<% end %>
用户模型
class User < ActiveRecord::Base
acts_as_messageable :table_name => "messages", # default 'messages'
:required => [:topic, :body], # default [:topic, :body]
:class_name => "ActsAsMessageable::Message", # default "ActsAsMessageable::Message",
:dependent => :nullify # default :nullify
end
消息控制器
class MessagesController < ApplicationController
def new
@message = ActsAsMessageable::Message.new
end
def create
@to = User.find_by_email(params[:acts_as_messageable_message][:to])
current_user.send_message(@to, params[:acts_as_messageable_message][:topic], params[:acts_as_messageable_message][:body])
end
end
开发日志
Started POST "/messages" for 127.0.0.1 at 2012-11-15 07:23:40 -0600
Processing by MessagesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"OqaDOP6PldbFVXWPZyijn+887Ym/fDsU0oqzVrL0rQA=", "acts_as_messageable_message"=>{"to"=>"xyz@test.com", "body"=>"test", "topic"=>"test"}, "commit"=>"Create Message"}
[1m[35mUser Load (0.5ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`email` = 'xyz@test.com' LIMIT 1
Completed 500 Internal Server Error in 3ms
NoMethodError (undefined method `send_message' for nil:NilClass):
app/controllers/messages_controller.rb:29:in `create
'