Use a global variable in my SqlDataSource tag - sqldatasource

Any suggestions how I can substitute a variable into my database connection string with a global variable in a SqlDataSource tag?
Like: <%$ ConnectionStrings:Production %>" ProviderName="<%$ ConnectionStrings:Production.ProviderName %>">
To this: <% Globals.s_dbConnectionString %>" ProviderName="<%$ ConnectionStrings:Production.ProviderName %>">

Related

How to write <noscript> tag in html.erb file

I have an app.html.erb and I need to add noscript tag in it. Like for adding a script tag we do something like
<%= javascript_tag do %>
<% end %>
I am not able to figureout how to write noscript tag in html.erb. Please help me.
<noscript>
<iframe src="googletagmanager.com/ns.html?id=#{key}" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
I think this should work:
<noscript>
<iframe src="googletagmanager.com/ns.html?id=<%= key %>" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
ERb will interpret things in <%= %> as ruby, so this would asume there's a variable key that was passed into the erb.

ERB String interpolation generating incorrect HTML syntax

I have these lines in a view:
<%
if current_user.is_fav?(a)
cls = "product fav"
else
cls = "product"
end
%>
and later
<div class=<%= cls %> >
This produces:
<div class="product" fav>
EDIT: as the answer says, it did NOT produce this, it was the browser "inspect" that showed it to me.
I can work my around it, but why doesn't the above code work?
This produces:
<div class="product" fav>
No, it doesn't. It produces <div class=product fav>. What you see (via something like "inspect element", correct?) is browser trying to interpret your broken markup as close to html spec as it can.
As noted in other answers, what you should do instead is something like this:
<div class="<%= cls %>">
<div class=<%= cls %> >
should probably be
<div class="<%= cls %>">
You're substituting a string into an erb placeholder, but the underlying template does not indicate that the placeholder itself is a string. ERB is likely making a best effort attempt to do a correction but it's oviously not able to cope. Edit: Sergio pointed out this is likely the browser doing this, not ERB.

Difference between th:with and th:if

what is the difference between th:with and th:if in thymeleaf? Do I use th:if or th:with? I am not too sure of their difference.
I have tried to search for usage of th:with but there isn't much document around.
For example,
<td>
<div th:with="${a given condition}"> <!--do I use th:if or th:with here?-->
<span th:if="${}"></span>
<span th:if="${}"></span>
</div>
</td>
th:with is used to assign variables. It doesn't have anything to do with evaluating conditions, instead it's used to assign a value to a variable. The sytnax looks like this:
<div th:with="firstPer=${persons[0]}">
th:if and th:unless are used to evaluate conditions (to determine whether or not to show a block of html).
<div th:if="${user.isAdmin()} == false">

Underscore Template with Ruby Backend not working

I am using the underscore template to create a template for making bootstrap dropdown menus. The code worked fine on my own computer, but now I'm adding it to a ruby on rails backend server. Now the code no longer works. I was told the code in between the <% %> is interpreted as ruby code instead of javascript. I don't know ruby at all, but someone showed me how to write a for loop.
<!-- Dropdown Menu-->
<script type="text/template" id="dropdown">
<div class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">
<span> <%= name %> </span>
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<% array_of_items.each do |obj| %>
<li> <a> <%= obj %> </a> </li>
<% end %>
</ul>
</div>
</script>
The error I get is "Undefined variable array_of_items" Of course it's not defined, I define it later on
this.$el.append(this.makeDropdown({
name: this.name,
array_of_items: [1,2,3,4]
}));
The underscore template works like this for Javascript, but for Ruby it will not allow me to have an undefined variable in a template for some reason, What can I do?
So the problem I think is here:
<% array_of_items.each do |obj| %>
Rails is interpreting this as ERB, and as there's no variable declared in Ruby, you're getting the error. Try adding your JS code into the asset pipeline perhaps?
Javascript's and ruby's variable are in quite different 'scopes';
here <% array_of_items %> should be defined as a template local variable:
<% array_of_items = [1,2] %>
<%= array_of_items %>
<script>
document.body.innerHTML += array_of_items;
array_of_items = ['j', 's'];
document.body.innerHTML += array_of_items;
// var from js + var from ruby
document.body.innerHTML += array_of_items + <% array_of_items %>; // #=> should be smth like `js12`
</script>

Can “field-with-errors” be attached to the parent of the input tag that raises the error?

So I have an input element like this. The wrapping element is about, you know, a visual thing.
<div class="input-wrap">
<input class="blah-blah" />
</div>
When the <input> contains the error, it'll be like this:
<div class="input-wrap">
<div class="field-with-errors">
<input class="blah-blah" />
</div>
</div>
But what I want to do is:
<div class="input-wrap field-with-errors">
<input class="blah-blah" />
</div>
I found this page, it's very close to my question
Rails 3: "field-with-errors" wrapper changes the page appearance. How to avoid this?
Now I know I can throw
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
"#{html_tag}".html_safe
}
to avoid making a wrapping tag around the <input> tag that has an error on. But what I really wanna do is, again, adding "field-with-errors" class on the direct parent of the <input> tag. Can I do that? Does ActionView hold the tree structure of DOM Nodes?
You can put the code for handling errors wherever you like, just call it as a block on the instance variable, for example
if #instance.errors.any?
<div class="field with errors">
#instance.errors.full_messages.each do |msg|
<p><%= msg %></p>
end
end
If you user this a lot, it's good to pull it out into a helper and pass in the instance variable as a parameter.

Resources