Rails Parameters from Get/Post

accessing POST parameters

The Model.new method takes a hash.

params[:product] actually contains something like {:serial => 555, :value => 111}

The url you would want to use is:

http://localhost:3000/products?product[serial]=555&product[value]=111

(Make sure that you are indeed using POST)

If you want to keep your current url scheme you would have to use:

@product = Product.new({:serial => params[:serial], :value => params[:value]})

You can also determine exactly what is available inside of params by printing it out to console using:

p params

Good luck!

Rails parameters from GET/POST

In Rails you don't have specific POST or GET parameters. You do have a POST or GET request. You can check it like this in your controller:

request.post?

or you can check for other HTTP verbs: GET, PUT and DELETE:

request.get?
request.put?
request.delete?

For more info, check this piece of the documentation: http://railsapi.com/doc/rails-v2.3.8/classes/ActionController/Request.html

How to get just GET and POST params as hash in Rails?

You should have permitted params (Strong parameters).

In your controller have permitted params method.

def your_params
params.permit(:query1, :query2)
end

If you wish to have a hash of those you can do

your_params.to_h #This will return hash of permitted params

Update:

Incase you have multiple query* type of parameters you can select them and permit!. Here is a command line explanation

# passing the list of parameters
> params = ActionController::Parameters.new({query1: 'aa', query2: 'bb', query3: 'cc', controller: 'users', action: 'index'})
=> <ActionController::Parameters {"query1"=>"aa", "query2"=>"bb", "query3"=>"cc", "controller"=>"users", "action"=>"index"} permitted: false>

# Select all the parameters of type query<integer>
> all_queries = params.select {|i| i.match?(/query\d/) }
=> <ActionController::Parameters {"query1"=>"aa", "query2"=>"bb", "query3"=>"cc"} permitted: false>

# mark the selected as permitted
> all_queries.permit!
=> <ActionController::Parameters {"query1"=>"aa", "query2"=>"bb", "query3"=>"cc"} permitted: true>

# get them as hash
> all_queries.to_h
=> {"query1"=>"aa", "query2"=>"bb", "query3"=>"cc"}

Controller method will look like

# getting all query<integer> like params
def your_params
params.select {|param| param.match?(/query\d/}.permit!
end

How can I get post parameter in controller

If you are getting an empty value for params[:deal_id] you are probably missing adding it to the allowed parameters which may not be part of the default fields for a Payment resource:

def params
params.require(:payment).permit(:deal_id)
end

Make sure you have the field in the params method on your payments controller.

Access old get parameters in URL after a post request

you should use RESTful routes so that you do not have to care about such issues.

since you are not posting much about the actual code or problem you are trying to solve, i can just assume what might be the issue here and how to solve it.

Identify GET and POST parameters in Ruby on Rails

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

Rails API Get with parameters

/api/v1/users/1

is not the same as

/api/v1/users?id=1

As can be seen from rake routes:

    api_v1_users GET    /api/v1/users(.:format)                 api/v1/users#index
POST /api/v1/users(.:format) api/v1/users#create
new_api_v1_user GET /api/v1/users/new(.:format) api/v1/users#new
edit_api_v1_user GET /api/v1/users/:id/edit(.:format) api/v1/users#edit
api_v1_user GET /api/v1/users/:id(.:format) api/v1/users#show
PATCH /api/v1/users/:id(.:format) api/v1/users#update
PUT /api/v1/users/:id(.:format) api/v1/users#update
DELETE /api/v1/users/:id(.:format) api/v1/users#destroy

api/v1/users/1 will route to api/v1/users#show. And api/v1/users?id=1 will route to api/v1/users#index with params: {'id'=>'1'}.

So, the routing is happening exactly as it is supposed to.

I'm not sure what 'other route' you would create. But you could do:

module Api
module V1
class UsersController < ActionController::API

def index
if params[:id]
user = User.find(params[:id])
render json: user, status: :ok
else
users = User.order('created_at');
render json: {status: 'Success', message: 'Loaded users', users: users}, status: :ok
end
end

def show
user = User.find(params[:id])
render json: user, status: :ok
end

end

end
end

But, that's really ugly and terrible.

Better to just format your URL correctly in iOS(swift).



Related Topics



Leave a reply



Submit