In label newline character is not working - ruby-on-rails

Hi i have the code like this
<label class="floating">
<input name="role[permissions][]" type="checkbox" value="project_reopen
asd">
Project reopen
Asd
</label>
In my Ruby code I put the value like this
puts 'project_reopen'+"\n"+'asd'.to_s.humanize
The value is displaying the line break but in view its not working
om/I7NDj.png

In HTML, like breaks are different. You should use <br> for that purpose:
<input name="role[permissions][]"
type="checkbox"
value="project_reopen<br>asd">
You should not expect any whitespace to be preserved in HTML as typed. Conseq spaces will be squeezed, etc.
To print the string that comes from not trusted source (like DB,) one should explicitly tell Rails it’s fine to use it as is:
value="<%= #string.html_safe %>"

Related

How to format date using message.properties on thymeleaf?

I'm using thymeleaf and I need to format this field to a specific date. It's working the way it is, but I need the date format to be into the message.properties.
So this is working:
<input type="text" id="dtFrom" class="form-control " placeholder="yyyy-MMM-dd" th:attr="placeholder=''+#{default.date.format}+''" autocomplete="off" th:name="startDate" th:value="${srchCmd.startDate}?${#dates.format(srchCmd.startDate, 'yyyy-MMM-dd')}">
</input>
But instead I need something like this:
<input type="text" id="dtFrom" class="form-control " placeholder="yyyy-MMM-dd" th:attr="placeholder=''+#{default.date.format}+''" autocomplete="off" th:name="startDate" th:value="${srchCmd.startDate}?${#dates.format(srchCmd.startDate, #{default.date.format})}">
</input>
When I select the date instead of having the date formatted on the text input I have this:
??welco12e.12essage_en_US??
How is the right way to use #{} inside ${}? Not sure if that's the problem though.
My usage is slightly different, but, I was able to get the basic concept to work using:
<span th:text="${#dates.format(timestamp, #messages.msg('timestampFormat'))}">10/31/2018 11:59:07 -0500</span>
where my messages.properties file contains a (valid) value for 'timestampFormat', and timestamp is an available Model attribute (type Instant, for java8).
Note that I had to use #messages.msg('key') explicitly here.

set textbox value with razor only uses numeric portion

I am setting the value "30 Day Report" to
<input type="text" value=#Model.rpt.ReportDescription.ToString() />
But the value ends up being only "30". I verified this using chrome developer tools. It works if I use #Html.TextBox() or #Html.TextBoxFor() so I know the value comes through, but I don't want to use any html helpers. How can I make the value display properly without html helpers?
Thank you.
You forgot the quotes " around the value:
<input type="text" value="#Model.rpt.ReportDescription.ToString()" />
// ^ ^

Difference between th:text and th:value in Thymeleaf

I just recently started using Thymeleaf through one of my projects. I have seen few examples where th:text=${example} is being used in some places th:value=${example}.
I have gone through the Thymeleaf documentation but couldn't find anything explicitly citing the difference, nor did any question on SO.
Any help would be really appreciated! Thanks.
th:value is modification of html attribute value.
For button, input and option elements, the value attribute specifies the initial value of the element
th:text is used for tag body modification.
div{background-color: lightblue; padding: 2px} // to highlight empty div
<!--th code: <div th:value="${value}"/></div> -->
<br/>Result th:value div: <div value="sometext"/></div>
<!--th code: <form><input th:value="${value}"/></form>-->
<br/>Result th:value form: <form><input value="sometext"></form>
<!--th code: <div th:text="${value}"></div>
Same as: <div>[[${value}]]</div> -->
<br/>Result th:text div: <div>sometext</div>
Here is docs of different Thymeleaf attributes features
Lets see an example:
<input type="radio" name="gender" value="male"> Male<br>
if we want to use thymeleaf in value portion of this input tag, then we will use,
<input type="radio" name="gender" th:value="${someValue}"> Male<br>
if we want to see the text (here Male) sent from the controller dynamically, then we use,
<input type="radio" name="gender" th:text="${someText}""> <br>
th:name => This would be the name of the value that you will be passing to another page (Exemplar scenario).
th:value => This would be the actual value that you would be passing. It could be obtained from a model or straight from the database explicitly.
<form th:action="#{confirm-pass-details.html}">
<button type="submit" th:name="event-id" th:value="${event.get().getEventid()}">Buy Passes</button>
</form>

Multiple variables with the same name in POST request

I was working on a webbot and I came across this strange page where multiple variables had the same name but different values in as shown by the Firefox web console. I am not sure as to how I can replicate this behavior in python. Currently, I am using the requests library to make post requests and that takes in a dictionary of name and value pairs. And of course, dictionaries have unique keys. So I was wondering if anyone could tell me how to send post requests with multiple variables carrying the same name.
sel_subj:dummy
sel_subj:ECE
Thanks,
Rajiv
Edit: Here is the html source that causes this
<input type="hidden" value="dummy" name="rsts"></input>
<input type="hidden" value="dummy" name="crn"></input><br></br>
<input type="hidden" value="120138" name="term_in"></input>
**<input type="hidden" value="dummy" name="sel_subj"></input>**
<input type="hidden" value="dummy" name="sel_day"></input>
<input type="hidden" value="dummy" name="sel_schd"></input>
<input type="hidden" value="dummy" name="sel_insm"></input>
<input type="hidden" value="dummy" name="sel_camp"></input>
<input type="hidden" value="dummy" name="sel_levl"></input>
<input type="hidden" value="dummy" name="sel_sess"></input>
<input type="hidden" value="dummy" name="sel_instr"></input>
<input type="hidden" value="dummy" name="sel_ptrm"></input>
<input type="hidden" value="dummy" name="sel_attr"></input>
<table class="dataentrytable" summary="Table is used to present the course search criteria">
<tbody>
<tr>
<td class="delabel" scope="row"> … </td>
<td class="dedefault" colspan="37">
**<select id="subj_id" multiple="" size="10" name="sel_subj"> …
</select>**
</td>
</tr>
</tbody>
</table>
Notice how the select tag and the highlighted input tag have the same name.
The only method that I know is using a variable-name appended with [].
<input type="hidden" value="dummy" name="sel_subj[]"></input>
<input type="hidden" value="ECE" name="sel_subj[]"></input>
This results in an array placed in $_POST['sel_subj'], with $_POST['sel_subj'][0] being "dummy" and $_POST['sel_subj'] being ECE.
Now as I think of it, I think the creation of the array is done by the php-parser when there is a [] attached. This suggests that both values are send through the POST even if there is not [] at the end of the name. Maybe PHP can be configured not to dismiss this values.
In case of GET variables (in the url), you can have multiple values with the same name. You can just parse the entire url and read and use every value.
PHP even solves this automatically if you add [] to the name of the parameter. In that case, it automatically changes it into an array. But this is a trick as well. It is fairly easy to write a piece of code that does the same thing with duplicate names without them having [] as a postfix.
They same will happen post variables as well. You just might need a little more code to read them properly.
The code in this case probably checks if there is sel_subj with any value other than dummy. If that is the case, then that value is used. If it doesn't exists, sel_subj may still exist with the value dummy. That is probably an indication for the script that the form was posted, but no value was selected.
So actually, I think it's quite easy to explain how this script works, and probably even why, but I don't think it's a very good solution to put all defaults in hidden fields this way, so I would suggest you don't try to replicate this solution. :-)

Why isn't Razor recognizing a variable without wrapping it in parenthesis in some cases?

I have a section of Razor code in one of my views that isn't behaving the way I expect. The code in question contains multiple locations where I need to append the value of a variable from the Model onto an html attribute. The code below is a slightly simplified version of the markup on my page (I've removed some of the markup 'noise' that wasn't important to this example and just made the code harder to read).
There are three instances where I use #topic.StandardTopicId. The 1st and 2nd instances get replaced as I expected, but the 3rd instance name="IsCovered_#topic.StandardTopicId" renders without replacing the variable.
Code in question:
#foreach(var topic in Model.Group) {
<div id="frame-#topic.StandardTopicId" class="topic-frame">
<label>
<input type="radio" name="IsCovered_#(topic.StandardTopicId)" />
No
</label>
<label>
<input type="radio" name="IsCovered_#topic.StandardTopicId" />
Yes
</label>
</div>
}
Sample of the output from the above code block:
...
<div id="frame-42" class="topic-frame">
<label>
<input type="radio" name="IsCovered_42" />
No
</label>
<label>
<input type="radio" name="IsCovered_#topic.StandardTopicId" value="No" />
Yes
</label>
</div>
...
Can anyone explain why the last instance of the variable is not being replaced and is instead being rendered as is?
Razor will not parse the "#" in the middle of a word. The underscore is treated as part of a word as opposed to the dash. Use #() to place Razor code within words. #(topic.StandardTopicId)
Here's a reference to Razor syntax: C# Razor Syntax Quick Reference
Most likely, Razor considers IsCovered_#topic.StandardTopicId a valid e-mail address format, and therefore leaves it as-is.

Resources