Make condition inside the render

0

I'm learning Ruby and am having a question about how to make a if inside the render to display param only if it exists ...

Ex:

if params[:field]
  puts 'field'  =>  params[:field],
end

How do I resolve this in this code?

render :json => {
    'request'=>{
        'negotiation'   =>  params[:negotiation],
        'state' =>  params[:state],
        'city'  =>  params[:city],
     },
    'response'=>lista,
}

abs!

    
asked by anonymous 19.05.2015 / 23:01

1 answer

1

Mount the hash before:

res = {
    'request'=>{ },
    'response'=>lista
}

Add the parameters like this:

res['request']['negotiation'] = params[:negotiation] if params[:negotiation]

res['request']['state'] = params[:state] if params[:state]

And render it

render json: res
    
20.05.2015 / 15:08