I passing ransack params from page to page. Initially params looks like hash
{"processed_at_gteq_datetime"=>"2021-08-01", "processed_at_lteq_datetime"=>"2021-09-14", "status_eq"=>"processed"}
But after sending this params to another page, it becomes a string and takes the following type:
"{\"processed_at_gteq_datetime\"=>\"2021-08-01\", \"processed_at_lteq_datetime\"=>\"2021-09-14\", \"status_eq\"=>\"processed\"}"
And in this form, I cannot use them for searching.
How i can convert this string:
"{\"processed_at_gteq_datetime\"=>\"2021-08-01\", \"processed_at_lteq_datetime\"=>\"2021-09-14\", \"status_eq\"=>\"processed\"}"
to hash?
I tried
JSON.parse my_params
But it doesn't work with this string
JSON::ParserError Exception: 767: unexpected token at '{"processed_at_gteq_datetime"=>"2021-08-01", "processed_at_lteq_datetime"=>"2021-09-14", "status_eq"=>"processed"}'
I guess you are passing parameters from page to page using either a link or a redirect.
If you want to keep those params as such, you have to pass them to the path helper:
link_to other_page_path(ransack_hash)
# => /other_page?processed_at_gteq_datetime=2021-08-01&processed_at_lteq_datetime=2021-09-14&status_eq=processed
and similar constructs. This
link_to other_page_path(q: ransack_hash)
# => /other_page?q[processed_at_gteq_datetime]=2021-08-01&q[processed_at_lteq_datetime]=2021-09-14&q[status_eq]=processed
will nest the params so that you can then retrieve in the other controller with
ransack_params = params[:q]
The reason this doesn't work:
JSON.parse("{\"foo\"=>\"bar\"}")
is because it's not valid Json. for valid JSON you need to replace the ruby => operator with the javascript : operator. You can do this with a ruby string manipulation.
string = "{\"foo\"=>\"bar\"}"
json = string.gsub(/=>/,':')
JSON.parse(json)
When sending params to a rails controller like from a test or a view
{ "a[b]" => true }
I receive it as
{"a" => {"b" => true}}
I would like to know how this happens
This happened because rails middleware convert this type of parameter into hash so that it is more easy to access and use. You can have more in depth info in Rails guide.
I need to pass parameters to a path so that the path looks like the following:
http://localhost/submission_app/submissions?search_submission_type=ISH&submission_status_arr[]=51
I tried
submissions_path(:search_submission_type => "ISH", :submission_status_arr[] => 51 )
But am getting wrong number of arguments (0 for 1..2) error message on my view page.
I then tried:
submissions_path("search_submission_type=ISH&submission_status_arr[]=51")
But this one gives me the following url (Note the dot instead of & before the argument)
http://localhost/submission_app/submissions.search_submission_type=ISH&submission_status_arr[]=51
How do I need to pass the parameters so that I get the correct format for the url?
Your suggestions are most appreciated. Thank you
Rails uses parameter[]=value to signify that parameter should be considered an array.
You just need to pass an array to the path helper to get rails to generate the path for you.
submissions_path(:search_submission_type => "ISH", :submission_status_arr => [51] )
Correct syntax is
submissions_path(:search_submission_type => "ISH", :submission_status_arr => [51] )
Try this:
submissions_path(:search_submission_type => "ISH", "submission_status_arr[]" => 51)
It seems that we will need 2 methods, one is CGI.escape and the other might be h (unless we hardcode &.
Is there a method that takes an array or hash of params, and compose it into this form?
src="foo.php?href=http%3A%2F%2Fexample.com%2F&layout=standard"
It is for Rails 2.2.2, so if there is a method in Rails 2.x that can do it?
I believe you can use Object#to_query to do it, but I'm not certain if it is available in Rails 2.x. For example:
{
:href => "http://example.com/",
:layout => "standard"
}.to_query # => "layout=standard&href=http%3A%2F%2Fexample.com%2F"
What is the simplest way to identify and separate GET and POST parameters from a controller in Ruby on Rails, which will be equivalent to $_GET and $_POST variables in PHP?
You can use the request.get? and request.post? methods to distinguish between HTTP Gets and Posts.
See http://api.rubyonrails.org/classes/ActionDispatch/Request.html
I don't know of any convenience methods in Rails for this, but you can access the querystring directly to parse out parameters that are set there. Something like the following:
request.query_string.split(/&/).inject({}) do |hash, setting|
key, val = setting.split(/=/)
hash[key.to_sym] = val
hash
end
You can do it using:
request.POST
and
request.GET
There are three very-lightly-documented hash accessors on the request object for this:
request.query_parameters - sent as part of the query string, i.e. after a ?
request.path_parameters - decoded from the URL via routing, i.e. controller, action, id
request.request_parameters - All params, including above as well as any sent as part of the POST body
You can use Hash#reject to get to the POST-only params as needed.
Source: http://guides.rubyonrails.org/v2.3.8/action_controller_overview.html section 9.1.1
I looked in an old Rails 1.2.6 app and these accessors existed back then as well.
There is a difference between GET and POST params. A POST HTTP request can still have GET params.
GET parameters are URL query parameters.
POST parameters are parameters in the body of the HTTP request.
you can access these separately from the request.GET and request.POST hashes.
request.get? will return boolean true if it is GET method,
request.post? will return boolean true if it is POST method,
If you want to check the type of request in order to prevent doing anything when the wrong method is used, be aware that you can also specify it in your routes.rb file:
map.connect '/posts/:post_id', :controller => 'posts', :action => 'update', :conditions => {:method => :post}
or
map.resources :posts, :conditions => {:method => :post}
Your PostsController's update method will now only be called when you effectively had a post. Check out the doc for resources.
I think what you want to do isn't very "Rails", if you know what I mean. Your GET requests should be idempotent - you should be able to issue the same GET request many times and get the same result each time.
You don't need to know that level of detail in the controller. Your routes and forms will cause appropriate items to be added to the params hash. Then in the controller you just access say params[:foo] to get the foo parameter and do whatever you need to with it.
The mapping between GET and POST (and PUT and DELETE) and controller actions is set up in config/routes.rb in most modern Rails code.
I think what Jesse Reiss is talking about is a situation where in your routes.rb file you have
post 'ctrllr/:a/:b' => 'ctrllr#an_action'
and you POST to "/ctrllr/foo/bar?a=not_foo" POST values {'a' => 'still_not_foo'}, you will have three different values of 'a': 'foo', 'not_foo', and 'still_not_foo'
'params' in the controller will have 'a' set to 'foo'. To find 'a' set to 'not_foo' and 'still_not_foo', you need to examine request.GET and request.POST
I wrote a gem which distinguishes between these different key=>value pairs at https://github.com/pdxrod/routesfordummies.
if request.query_parameters().to_a.empty?