0

我正在尝试将行为用作能够发送消息的 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

'

4

2 回答 2

1

从错误消息和提供的代码看来,您的 params obj 的格式不像您想的那样。试一试params[:acts_as_messageable_message],而不是[:message]. 如果这不起作用,请检查日志以查看作为参数传入的内容。

于 2012-11-15T06:59:41.273 回答
0

我最终让它工作了。最终的问题是 gem 使用的是用户模型,并且期望控制器是用户控制器,而不是另一个名为“消息”的控制器。所以我只是将我的操作移到了我的用户控制器中,添加了路由并更改了视图路径,它现在可以工作了。@Alex.Bullard 感谢您的帮助。

我在下面发布我的编辑:

控制器变更

class UsersController < ApplicationController
  respond_to :html, :json
  def new_message
    @message = ActsAsMessageable::Message.new
  end

  def create_message
    @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])
    redirect_to :back, notice: "Message sent to Owner"
  end

end

看法

            <%= simple_form_for ActsAsMessageable::Message.new, :url => create_message_users_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 %>

路线

  resources :users, :except => [ :create, :new ] do
     get "new_message", :on => :collection
     post "create_message", :on => :collection
     resources :store
  end
于 2012-11-15T14:15:09.870 回答