Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to not use Rails action parameter in controller

I am implementing a third party API for Shipworks in a Rails server and the Shipworks client app is posting an action param with Shipworks specific semantics.

However the Rails routing logic overwrites this param to be the name of the controller method.

Is there a custom route I could write to get the value of that action param without it being overwritten to be the name of my controller method?

like image 997
John Wright Avatar asked Jun 02 '11 02:06

John Wright


People also ask

What is action controller Rails?

Action Controllers are the core of a web request in Rails. They are made up of one or more actions that are executed on request and then either it renders a template or redirects to another action.

Are Rails params always strings?

Note that parameter values are always strings; Rails does not attempt to guess or cast the type.

What are strong parameters in Rails?

Strong Parameters, aka Strong Params, are used in many Rails applications to increase the security of data sent through forms. Strong Params allow developers to specify in the controller which parameters are accepted and used.

What is params in Rails controller?

Specifically, params refers to the parameters being passed to the controller via a GET or POST request. In a GET request, params get passed to the controller from the URL in the user's browser.


2 Answers

I fell into this trap today and came with this solution in controller method. It's Rails 4.1:

if request.method == "POST"
  action = request.request_parameters['action']
else
  action = request.query_parameters['action']
end
like image 95
Petr Avatar answered Sep 26 '22 01:09

Petr


I figured it out. There is a raw_post method in AbstractRequest.

So you can do this to parse the raw post params:

def raw_post_to_hash
  request.raw_post.split(/&/).inject({}) do |hash, setting|
    key, val = setting.split(/=/)
    hash[key.to_sym] = val
    hash
   end
end

and then just call raw_post_to_hash[:action] to access the original action param or any other param. There is probably an easier way.

like image 26
John Wright Avatar answered Sep 25 '22 01:09

John Wright