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?
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.
Related
I have a knockout value binded textarea like so:
<textarea class="form-control" data-bind="value: test" name="test" rows="4"></textarea>
However, when saving data with line breaks or extra spaces, upon next viewing of the page it displays it with the ASCII characters. Like so:
Overview being explained here in this box.
Test stuff test stuff.
When I look at the value in the database it does not have those characters:
Overview being explained here in this box.
Test stuff test stuff.
Indeed even in the view model before the data gets passed to the view everything looks correct:
Overview being explained here in this box.\n\nTest stuff test stuff.
And finally even when first entered, the knockout property pageModel().test() shows the text like this too:
"Overview being explained here in this box.
Test stuff test stuff."
So why does knockout finally render it with the ASCII characters? My KO model looks like this when the value is already input:
self.test = ko.observable('Overview being explained here in this box.
Test stuff test stuff.');
Okay, it's an MVC issue then, because I am calling it like so:
self.test = ko.observable('#Model.Test');
But I can't figure out how to get this to work, if I use Html.Raw the line breaks are interpreted correctly but then that breaks the javascript breaks because there is a space between lines without proper string ending.
I also try just escaping the \r\n characters:
self.test = ko.observable('Overview being explained here in this box.\\n\\nTest stuff test stuff.');
This doesn't remove the problem though, I still get the double
.
How the heck can I get this working right while still using the value binding? Because I am using knockout.validation, and I could make this work by using html binding and use a slight hack:
<textarea class="form-control" data-bind="value: test, event: { keyup: function(data, event) { data.test(event.target.value); } }" name="test" rows="2"></textarea>
But this completely breaks knockout validation for any textareas because html is a one way binding that doesn't get validated. So I still need to use the value binding.
How to get this working?
You're probably looking for
self.test = ko.observable('#HttpUtility.JavaScriptStringEncode(ViewBag.Test)');
or as you pointed out for .NET Core you'll need
#(JavaScriptEncoder.Default.Encode(question.Test))
I'm trying to render a Block's Field as Plain Text as I need it used as part of HTML, I've tried using |RAW however I read it was unstable + it didn't work haha!
This is my existing HTML minified
Read More
However I would like to make it more useable
Read More
This would mean that when a user modifies the DrupalBlock HEX code it would change the color of the box. However the issues is when it's printed on the page it's looking like this
<div data-quickedit-field-id="#" class="field field--name-field-color field--type-string field--label-hidden field--item quickedit-field">FFFFFF</div>
the only thing I would like printed is "FFFFFF" with no div's
-
Here is my question: How do I display my Field_color as plain text when it prints?
You can use |raw : {{ content.field_color|raw }}.
If you need more information please ask.
I suggest you do a dump or kint of your content.field_color variable. You might be able to get some more information on it and get the answer!
Anyway, we have something similar in our project and the way we do it is by using a .getString() method.
{% set image_align = content.field_image_align['#items'][0].getString() %}
<div class="{{ image_align }}">
Our field is a list of values so you'll have to look for another array item to call the .getString() method on.
I have something like:
<input type="text" name="TerrMng" id="TerrMng"/>
in HTML. What is the equivalent of the above using #Html.Display?
I tried using:
#Html.Display("TerrMng", TerrMng)
but was not successful. Note that I like to use #Html.Display but not sure how to translate the ID value so that it shows up.
This should do the trick for you. Adding the TerrMng as a 2nd parameter sets the value of the created html but the variable must come from your Model on load.
#Html.Display("TerrMng");
I am using Ruby on Rails 3.0.7 and I would like to populate the href attribute value of a HTML a tag in a view file.
Test name
I plan to retrieve that value (the "<populating_value>") from a input form text field.
What is the safest process (steps from validation to outputting in the view file) to make that? Where can I find deep information about this matter? What do you advice (for example: is it safe to populate the href attribute value without a previous security check on the string entered by a user?)?
Note: the value can be an URL or an e-mail address.
whats wrong with using link_to and escaping the string? If you are unsure of the strings safety call something like link_to "Test name", h(link) See http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to for more information.
This string should be escaped even without the call to h. Try reading this blog post on the XSS protection in Rails.
Prototype's Ajax accepts a parameter called 'parameters' with an hash array for parameters (prototype doc) which would automatically be sent as GET or POST vars, but I could not find how to add items to that array using the Rails button_to_remote method.
I could just add the parameters to the URL sent to the method, but that feels hackish.. Is there a better solution out there?
I actually found the solution! You can pass parameters in the function using the :with option, like this:
<%=button_to_remote "+3", {:url =>task_path(#project, #story, task), :with=>"'actual=3'"}%>
The trick is that the value for :with is a javascript expression that should return a key-value pair in the URL format, like "key=value". That's why there's extra quotes around the value on the same above.
A function could also be used to pull the information from the page, if necessary:
:with=>"getValuesForPostbackFunction()"
The function will be evaluated before the form is submitted.
Using button_to_remote the only way to send parameters to the next action is putting them into the URI.
button_to_remote is intended as a functional equivalent of link_to_remote, which also has no other way of adding parameters.
If you need more fine-grained control, you need to build the full form and submit that to your action.
Add hidden input fields in the form that you're calling the button_to_remote on.
<input type="hidden" name="name" value="myValue">