-1

I have a little problem with my ruby/rails application.

I want to get the ID from my selected item and make a query with them.

In my _form I have:

<%= f.label "Stock", for: "text" %>
<%= f.select :stock_id, @stocks_list, {},  onchange: 'ShowUser(this.value)' %>

And my JavaScript code is:

function ShowUser(str){
   alert("You select item with value: " + str);
}

I need catch 'str' value on my controller and show the information of the selected sotck dynamically (by a query).

update:

When I select a item just show the message in a pop-up and nothing else. My complete function is

function ShowUser(str)
{

    alert("You select item with value: " + str);

    JQuery.ajax( { 
        data: {valor: str},
        type: 'post',
        url: "/orders/create",
    });

}

and the console log is

Started GET "/orders/new" for 127.0.0.1 at 2014-11-25 16:54:39 -0600
Processing by OrdersController#new as HTML
Staff Load (0.2ms)  SELECT `staffs`.* FROM `staffs`
Provider Load (0.2ms)  SELECT `providers`.* FROM `providers`
Stock Load (0.2ms)  SELECT `stocks`.* FROM `stocks`
Rendered public/templates/menu.html.erb (1.0ms)
Rendered orders/_form.html.erb (6.8ms)
Rendered orders/new.html.erb within layouts/application (39.7ms)
Completed 200 OK in 273ms (Views: 268.3ms | ActiveRecord: 0.7ms)

when I select an item the console doesn't change

2 Answers2

0

Maybe you should X-CSRF-Token in POST request headers?

Yoihito
  • 348
  • 2
  • 13
0

The reason your request isn't working is because your using the wrong syntax. It is trying to find a variable called JQuery hence why your getting the error Uncaught ReferenceError: JQuery is not defined.

The correct syntax is either jQuery.ajax or $.ajax.

After correcting that, you should create a response within the controller that your request is pointing to to confirm in realtime that its all working. But you should see the request in your rails console.

On another note, I noticed in the comments that your using chrome and its developer console. I would highly recommend using firefox and its plugin firebug for all your debugging. I find it much better then chrome and its very easy to see errors in your code like the one thats in this code.

doz87
  • 591
  • 6
  • 15