-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

11
  • You have to make a request to the server. How are you making such request? Commented Nov 25, 2014 at 22:03
  • I tried this JQuery.ajax( { data: {valor: str}, type: 'post', url: "/orders/create", }); from this link Commented Nov 25, 2014 at 22:11
  • And when you tried that, what happened? Commented Nov 25, 2014 at 22:12
  • Rails is server side. Javascript is client side. You need to send an Ajax request to achieve the result your after Commented Nov 25, 2014 at 22:13
  • @AlexWayne does nothng. Commented Nov 25, 2014 at 22:15

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.