This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to use ternary operator in razor (specifically on HTML attributes)?
I am trying to do the following but its erroring so I'm obviously doing something wrong with the Razor syntax:
<td>#{item.Licence.MachineId != null ? #:"TB Master" : #:"HandHeld"} </td>
The following should work:
<td>#(item.Licence.MachineId != null ? "TB Master" : "HandHeld")</td>
Try this
<td>#(item.Licence.MachineId != null ? "TB Master" : "HandHeld")</td>
Related
This question already has answers here:
ruby using the "&:methodname" shortcut from array.map(&:methodname) for hash key strings rather than methodname
(3 answers)
Closed 3 years ago.
I have a rails app that does a lot of JSON parsing (ie using strings as keys rather than symbols).
I have the following code:
ad_source_ids = []
logged_one['migrated'].each { |mig| ad_source_ids << mig['id'] }
I'd like to do
ad_source_ids = logged_one['migrated'].map(&:id)
but don't think I can. What is an alternative? I'd like to removed the ad_source_ids tmp variable.
You're almost there. Try this:
ad_source_ids = logged_one['migrated'].collect { |mig| mig['id'] }
This question already has answers here:
HAML - what does the "!=" operator do?
(3 answers)
Closed 4 years ago.
I discovered the following statement in a haml template:
!= render partial: 'path/to/partial'
What is the purpose of != in this context?
It is used for Unescaping HTML.
example:
= "<Hello>" will give <g;Hello>
but now using
!= "<Hello>" will give <Hello>
I am trying to populate a text field on my GSP as such:
<label>Phone(aaa-bbb-cccc):</label> <g:textField name="phone" style ="border-radius: 5px"
value="${recordToEdit.telephones = [] ? null : recordToEdit.telephones.first()}"></g:textField><br>
but it still tell me I can't access first() on an empty list. telephones is a List of Strings each being a phone number.
as #gross-jonas pointed out, the recordToEdit.telephones = [] ? .. : .. is terribly wrong already, unless it's a typo
the check you are trying to make should look like:
value="${recordToEdit.telephones ? recordToEdit.telephones.first() : ''}"
or
value="${recordToEdit.telephones?.getAt( 0 ) ?: ''}"
You can just use the Null Safe Operator (?.) as
${recordToEdit.telephones?.first()}
for null checks, which is not sufficient.
UPDATE
for empty list checks and null checks,
${ recordToEdit.telephones ? recordToEdit.telephones[0] : '' }
will be good.
Dude, didn't you just mean == instead of = ?
It looks like you are overwriting your telephones which get issued successfully instead of comparing it.
This question already has answers here:
Rails ActiveRecord where or clause
(4 answers)
Closed 9 years ago.
i have the following query in my app:
Page.where(published: false).where("published_at > current_date").where("publication_end IS NULL OR publication_end < current_date")
Now i want to change this where to or, I was trying:
Page.where((published: false) || ("published_at > current_date"))
but it not working. Is there any way to do it?
This should work:
Page.where('published = ? OR published_at > current_date', false)
Can someone please tell me how to convert this to razor view syntax?
<%= ViewData.Model.Firms.Count > 0 ? Html.Pager(ViewData.Model.Firms.PageSize, ViewData.Model.Firms.PageNumber, ViewData.Model.Firms.TotalItemCount) : string.Empty%>
thanks for help in advance.
#if (Model.Firms.Count > 0)
{
#Html.Pager(
Model.Firms.PageSize,
Model.Firms.PageNumber,
Model.Firms.TotalItemCount
)
}
and if you want one liners:
#MvcHtmlString.Create(
Model.Firms.Count > 0
? Html.Pager(
Model.Firms.PageSize,
Model.Firms.PageNumber,
Model.Firms.TotalItemCount
).ToHtmlString()
: string.Empty
)
but personally I find the first far more readable.
You may want to simply make a helper instead that will take care of that logic, so your view can simply call #PagerTable(Model.Firms) which will figure out to write an empty string or call Html.Pager for you.