Tony Marshle
Tony Marshle

Reputation: 127

Passing html object properties into embedded ruby code

How can I pass html object properties into embedded ruby code in an html.erb file?

Lets say I have a ruby method A that accepts a string parameter(and also the return value of A is string). I think of scenarios like the following:

<input type="text" id="t" value="Leaves">
<%= A(document.getElementById("t").value) %>

Obviously I can't write code that way.

I want to pass the value/text of the textbox into method A and print A's return value into the html body. How can I do that?

Also, if I want to continuously check the value of the textbox and append A's return value(when passed the current value of the textbox to A) to the body of the document, what should I do? And if I instead wanted to set some paragraph p's text to this return value, what should I have done?

Upvotes: 0

Views: 266

Answers (1)

max
max

Reputation: 101811

You can use a HTML parser like Nokogiri.

frag = Nokogiri::HTML.fragment('<input type="text" id="t" value="Leaves">')
frag.at_css('#t').attr('value')

But it seems like a rather silly and overcomplicated solution to something that most likely can be solved by not using HTML strings to pass around data in your views / helpers in the first place.

Upvotes: 1

Related Questions