-1

Lets say I wanted to make a function to return someone's age from a rails database.

function getName(name_input) {
return <%= Names.find_by(name: name_input).age %>
}

When I've tried similary functions is seems that rails doesn't have access to local variables. Is there a workaround?

0

3 Answers 3

1

getName is a Javascript function that gets called on the client side. Code in <%=%> is executed at the server before sending the HTML view to the client; at that moment, Rails has no idea what name_input is.

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

Comments

0

Please try using <%= Names.find_by_name(name_input).age %>

Comments

0

<% %> are erb tags which embed Ruby inside HTML that your rails server will render and send to the client as pure HTML.

You cannot mix server-side ruby with client-side javascript.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.