Filipe Machado Carneiro Filipe Machado Carneiro - 1 month ago 8
Ruby Question

param is missing or the value is empty for:

I have a POST request from an api

I do not want to save anything in my application,
i would just like to send a request to the data and get an ok

my script

<script type="text/javascript">
$('input[type=submit]').click(function(e){
e.preventDefault();
var value = $('#form').serialize()

console.log(value);
$.ajax({
url : 'https://1broker.com/api/v1/order/create.php?',
type: 'post',
dataType : 'html',
data: value,
beforeSend : function(){
$('.load').show('100');
},
success : function(pre){
$('.load').hide('100');
}
});
});
</script>


here is my controller

private

def set_position
@position = Position.find(params[:id])
end


def position_params
params.require(:position).permit(:symbol, :margin, :direction, :leverage, :order_type, :referral_id).permit(nil)
end


end

and the form

<form class="form-horizontal" id="form" method="post">
<select class="form-control" name="symbol">
<option> </option>
<option>USDJPY</option>
<option>EURUSD</option>
</select>
<input type="text" class="form-control" name="margin">
<select class="form-control" name="direction">
<option> </option>
<option>long</option>
<option>short</option>
</select>
<input class="form-control" name="leverage">
<input type="hidden" name="order_type" value="market">
<input type="hidden" name="referral_id" value="9918">
<input class="form-control" name="stop_loss">
<input class="form-control" name="take_profit">
<button type="submit" class="btn btn-primary">Send</button>
</form>


in applicationController i'm using

protect_from_forgery with: :null_session


enter image description here

Answer

I believe the problem is that your ajax call doesn't match what your strong params are searching for.

  $.ajax({
    url : 'https://1broker.com/api/v1/order/create.php?',
    type: 'post',
    dataType : 'html',
    data: value,
    beforeSend : function(){
      $('.load').show('100');
    },
    success : function(pre){
     $('.load').hide('100');
    }
  });


  def position_params
    params.require(:position).permit(:symbol, :margin, :direction,:leverage, :order_type, :referral_id).permit(nil)
  end

You could either change your ajax call like Gokul M said

  data: { position: value }

or you could modify your strong params to not require position if you don't need to for other actions:

  def position_params
    params.permit(:symbol, :margin, :direction,:leverage, :order_type, :referral_id).permit(nil)
  end