Rails作为Messageable设置Form

时间:2012-11-15 01:30:22

标签: ruby-on-rails forms controller

我正在尝试使用acts as message able gem而我正在关注他们的示例controller

已解决,请参阅答案

在尝试在视图中发送消息时,我继续收到此错误未定义的方法`send_message'为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

2 个答案:

答案 0 :(得分:1)

从错误消息和提供的代码看起来像你的params obj没有像你想象的那样格式化。尝试params[:acts_as_messageable_message]而不是[:message]。如果这不起作用,请检查日志以查看以params传递的内容。

答案 1 :(得分:0)

我最终得到了它的工作。问题最终是gem正在使用User模型,并且期望控制器是User控制器而不是另一个名为“Messages”的控制器。所以我只是将我的动作移动到我的用户控制器中,添加了路径并更改了视图路径,现在它可以工作了。 @ 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