rails - is there a way to capture an entire POST's params?
I want to capture an entire posts params, store it in the DB in one field (text), and then later get at each individual param? Possible? Any example 开发者_如何学Goyou can show? thanks
You can serialize the entire params hash (or any other object)
class SomeModel < ActiveRecord::Base
serialize :params
…
end
class SomeModelsController < Applicationcontroller
def some_action
SomeModel.create(:params => params)
end
end
All you need is something like the following. I didn't include all of the Sendgrid params since there are so many and you will know what I mean with a few:
class SendgridMessage < ActiveRecord::Base
serialize :attachments
...
end
class SendgridMessagesController < ApplicationController
def create
SendgridMessage.create(:to => params[:to], :from => params[:from], :attachments => params[:attachments])
end
end
Sendgrid will send a POST to /sendgrid_messages with the params, and your object will be created with all of the correct fields (you will need to add some to the example) and serialized attachments like you are looking for.
perhaps
request.raw_post
is what you're looking for?
http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-raw_post
精彩评论