0

I'm trying to pass database value retrieved by ruby to javascript,to handle some div display.

Strange thing is,when I pass number(means I retrieve number from database and pass to js),it works fine,as follow,this load_web is a short int.

if (<%= @device.load_web %>==0)
{ $('.somediv').find('input,textarea,select').attr('disabled',true); }
});

But when I try same method,passing string to js,it returns me HTMLDIVElement,which is very strange.

if(<%= @device.brand%>=="apple")
    {
    alert("apple"); 
    }
    else
    {
    alert(<%= @device.brand %>);
    }

The data in my database for this colum is "apple",but the logic falls into else loop,and give me a "object HTMLDIVElement" message.

Why is so?

EDIT: I try use alert(<%= @device.brand %>.innerHTML); and get the following message: enter image description here

1 Answer 1

1

Remember that erb tags just insert the result of the ruby code in them into your html template. So unless @device.brand is returning some properly formatted javascript (or a number), you probably want to wrap it in strings. Try this:

if("<%= @device.brand%>"=="apple")
    {
    alert("apple"); 
    }
    else
    {
    alert("<%= @device.brand %>");
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I try change according to your solution,then the whole if statement not executed,neither go if nor else,(previously I go to else state).so I feel its not the quotation marks' problem
Was there any error messages in your javascript console?
thank you,Today I try again use " " instead of ' ',it works now!
I'm sure the choice of quotes does not matter to javascript. Glad it solved your problem.

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.