So I'm extremely confused on this. I have this coffeescript:
jQuery ->
$('.message').click (e) ->
e.preventDefault()
href = $(location).attr('href')
vote = { 'message': 'message': $('.messagebody').val(), 'photo_id' : href.substr(href.lastIndexOf('/') + 1)}
$.ajax '../messages/',
type: 'POST'
dataType: 'json'
data: JSON.stringify(vote)
error: (jqXHR, textStatus, errorThrown) ->
# console.log(jqXHR)
# console.log(textStatus)
console.log(errorThrown)
success: (data, textStatus, jqXHR) ->
$('.messagebody').val('')
console.log(vote)
When I fire this event my rails server receives this:
{"{\"message\":{\"message\":\"asdfasdfasdfasdfasdfasdfasdf\",\"photo_id\":\"1\"}
}"=>nil, "controller"=>"messages", "action"=>"create"}
And this is my controller that receives this parameter:
class MessagesController < ApplicationController
before_filter :authenticate_user!
def create
@message = Message.new()
logger.debug JSON.parse(params)
@user = User.find_by_id(Photo.where(:id => params[:message][:photo_id]).pluck(:user_id))
@message.photo_id = params[:message][:photo_id]
@message.message = params[:message][:message]
@message.user_id = @user.id
@message.user_email = current_user.email
@message.to_user = @user.email
UserNotifier.send_message(@message).deliver
respond_to do |format|
format.json { render json: '{"message": ok}' , status: :created}
end
end
end
When I try to parse the parameters I get this error
TypeError (no implicit conversion of ActionController::Parameters into String)
I've also tried not stringifying the data and just sending in the json, which my server is 100% fine with but the javascript returns this error:
SyntaxError: Unexpected token o
I'm not really sure what the issue is but I just need to resolve on of the errors and I've been trying to figure this out for hours and haven't found a solution yet.