Access params inside params? - ruby-on-rails

Form on submit sends params[:model], which inside have other hashes, like :attribute_1=>value_1
For example, server log:
Started POST "/tests/1/create_answer" for 127.0.0.1 at 2013-05-26 04:36:09 +0400
Processing by TestsController#create_answer as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sFZIbaxVSnf1auEbsGMdVVl7dg0OWXKnFA0pl1aXA9o=", "answer"=>{"user_answer"=>"ssss", "position"=>"96", "key_id"=>"2"}, "commit"=>"", "id"=>"1"}
Is there way to acces any of this attributes in controller, some kind of:
#key=Key.find(params[:answer][:key_id])
Tried this, returns nil.

Related

What's the actual URL Rails generated for HTTP Verb PATCH?

Please bear with a newbie. I understand how Rails provides simple request with GET for simple URL links like localhost:3000/rooms/11/listing. The format is straight forward as stated in the Routes table. However I am confuse when it comes to PATCH, PUT, DELETE & CREATE. For example the output below, with params, was when I clicked SAVE button. My question, what's the actual URL that Rails generated when I clicked that SAVE button?
Started PATCH "/rooms/11" for 127.0.0.1 at 2019-08-20 05:25:32 +0800
(0.8ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
↳ /usr/local/rvm/gems/ruby-2.6.0/gems/activerecord-5.2.2/lib/active_record/log_subscriber.rb:98
Processing by RoomsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"JHiCJ6HoTsgd8SGgLTFiZ9+J9hS9U8hHGvjKf4Lz3uieQ8OO2eFqFEM/D5xocHp/Nd3eA0az9k+okrmNe65BYg==", "room"=>{"home_type"=>"Apartment", "room_type"=>"Private", "accommodate"=>"3", "bed_room"=>"4", "bath_room"=>"3"}, "commit"=>"Save", "id"=>"11"}
I know from console
app.room_path(11)
=> "/rooms/11"
Is this Rails generated URL localhost:3000/rooms/11{"utf8"=>"✓", "authenticity_token"=>"JHiCJ6HoTsgd8SGgLTFiZ9+J9hS9U8hHGvjKf4Lz3uieQ8OO2eFqFEM/D5xocHp/Nd3eA0az9k+okrmNe65BYg==", "room"=>{"home_type"=>"Apartment", "room_type"=>"Private", "accommodate"=>"3", "bed_room"=>"4", "bath_room"=>"3"}, "commit"=>"Save", "id"=>"11"}?
No, the URL generated by app.room_path(11) is http://localhost:3000/rooms/11.
PATCH, PUT, DELETE and POST are called HTTP verbs. CREATE is not an HTTP verb.
One of these verbs goes along with your request, and Rails Router uses it to route the request to the correct Controller and Action.
Requests can have parameters, likes the one you showed here:
{"utf8"=>"✓", "authenticity_token"=>"JHiCJ6HoTsgd8SGgLTFiZ9+J9hS9U8hHGvjKf4Lz3uieQ8OO2eFqFEM/D5xocHp/Nd3eA0az9k+okrmNe65BYg==", "room"=>{"home_type"=>"Apartment", "room_type"=>"Private", "accommodate"=>"3", "bed_room"=>"4", "bath_room"=>"3"}, "commit"=>"Save", "id"=>"11"}
When you clicked on the Save button your browser requested http://localhost:3000/rooms/11 using the HTTP verb POST. The parameters were encoded on the body of the request.
A good place to learn more about this would be the Rails routing guide.
Here is the result from rake routes command. As you can see, GET, PATCH and PUT share the same generated URL (/rooms/:id, in your example /rooms/11). Since Rails 4.0, PATCH is the default verb to update action. Update action is triggered when you're sending a form to a route.

Improperly accessing parameters

The following action is receiving parameters
Started POST "/transactions/214/success?locale=it" for 5.171.1.159 at 2018-10-05 13:30:54 +0200
Processing by TransactionsController#success as HTML
Parameters: {"response-signature-base64"=>"JgO1UzlOubzB0Ic4qSaphxJm4hrFbeR/vF6B/ZanP1s=", "response-signature-algorithm"=>"HmacSHA256", "locale"=>"en", "id"=>"214"}
However, the cation is not properly processing the parameters. As a way of proof, the controller action has:
def success
puts params
puts "reponse signature base 64"
puts params[:'response-signature-base64']
but the log is not writing any of the above parametric data. What should I be doing to properly access these parameters?
Compare
Started POST "/transactions/214/success?locale=it"
Processing by TransactionsController#success as HTML
with code
def payment_success
You have a route mismatch.
EDIT
puts writes to stdout, not to rails log.

After renaming a model, why do my create and update functions fail?

I renamed one of my models and its associated table name, controller, view folder, and references of the old name throughout all files in the app. The app runs fine except I'm now unable to create or update Actions (new name) because of an error related to params. Here is the error received when creating a new Action:
undefined method `permit' for "create":String Did you mean? print
Here are the params shown with this error:
Parameters:
{"utf8"=>"✓", "authenticity_token"=>"[removed]", "commit"=>"Create This Campaign"}
I manually replaced the token with [removed] here.
I receive the same error when trying to update an Action:
undefined method `permit' for "update":String Did you mean? print
And here are the parameters shown with this update error:
Parameters:
{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"[removed]", "commit"=>"Submit", "id"=>"50"}
Before I renamed everything, these errors did not appear. Any idea why this is occurring? It looks like my app is passing a string (instead of a hash) to params.require(:action).permit, but I'm not sure why it would be doing that.
You shouldn't use action as a resource name in Rails. The action parameter in params is always set to the name of the action being invoked, meaning you cannot use params[:action] to post data back to your server.
In a controller's update action, params[:action] will always be the string "update", hence the error you're getting about permit not being defined on the string "update".

Capture response sent to webhook rails?

I'm working with my first webhook in my rails application. My HookController is receiving the following response:
Started POST "/hooks/callback" for 127.0.0.1 at 2014-11-18 14:10:13 -0500
Processing by HooksController#callback as HTML
Parameters: {"Status"=>"<?xml version=\"1.0\"?>\n<BackgroundReports userId=\"username\" ...
Completed 401 Unauthorized in 1ms
I'm trying parse the XML that is returned in the parameters and I'm getting stuck.
I have tried starting with the following in my controller raising :
webHook = JSON.parse(params[:status])
raise webHook.inspect
But it appears as if its not capturing the params.
How do I capture the params and parse the XML in my controller?
I was able to troubleshoot the problem myself.
I am using Devise for my User model. That was driving the 401 Unauthorized error. As a result I was not able to to pull and parse the params.
I found the answer in a similar question -> Webhook returning 401 in Rails on Stakoverflow. Adding the following allowed Devise to authenticate the response. I could parse the params from there:
skip_before_filter :verify_authenticity_token, :authenticate_user!

"Params" not working to handle JSON parameters in Rails

I have a webservice application, which in one of my controllers there's an action that receives a POST from an external application.
Here's the log from my heroku app, which confirms that it's coming a Json data parameter:
2012-01-05T18:26:07+00:00 app[web.1]: Started POST "/let" for 192.168.1.145 at 2012-01-05 10:26:07 -0800
2012-01-05T18:26:07+00:00 app[web.1]: Processing by LetController#create as HTML
2012-01-05T18:26:07+00:00 app[web.1]: Parameters: {"Id"=>"dfe510c1-56e2-48ae-af0d-c4b265798f2e", "FormId"=>"5025", "Answers"=>[{"IdQuestion"=>"cfe0"
, "Value"=>"One", "FilledDate"=>"/Date(1325795160323-0200)/"}], "MSISDN"=>"8984223722"}
So, how can I access this json parameter on my method? I've tried lots of things like params[:formid], params["formid"], ActiveSupport::JSON.decode(request.body) etc etc.. and when pass to view, nothing is rendered, it's like it doesn't get anything.
It looks as though you should be able to access your params as follows:
params[:Id]
params[:FormId]
params[:Answers]
params[:Value]
params[:FilledDate]
params[:MSISDN]

Resources