Reputation: 1203
I'm trying to use acts as message able gem and I'm following their example controller
SOLVED See Answer
I keep getting this error undefined method `send_message' for nil:NilClass when trying to send a message in the view
How should I adjust my code? Thanks
View (Form)
<%= 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 %>
User Model
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
Messages Controller
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
Development Log
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"=>"[email protected]", "body"=>"test", "topic"=>"test"}, "commit"=>"Create Message"}
[1m[35mUser Load (0.5ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`email` = '[email protected]' 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
'
Upvotes: 0
Views: 795
Reputation: 1203
I ended up getting it working. The problem ultimately was the gem was using the User model and was expecting the controller to be the User controller not another controller called "Messages". So I simply moved my actions into my Users controller, added the routes and changed the view path, and it now works. @Alex.Bullard thanks for the help.
I'm posting my edits below:
Controller Change
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
View
<%= 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 %>
Routes
resources :users, :except => [ :create, :new ] do
get "new_message", :on => :collection
post "create_message", :on => :collection
resources :store
end
Upvotes: 0
Reputation: 5563
From the error message and provided code seems like your params obj is not formated like you think. Give params[:acts_as_messageable_message]
a try instead of [:message]
. If that doesn't work check the log to see what is being passed in as params.
Upvotes: 1