yield in Ruby on Rails seems to add extra space when rendering - ruby-on-rails

I've got a problem with layouts and yield. When I follow the guides, I got extra space before the p tag. When I inspect the code with google chrome I got something like:
"
"
between the body and p tags. I type the same code as on the guide but it is not working, did I miss something?
Code for layout:
<html>
<head>
<title>Title</title>
</head>
<body>
<%= yield %>
</body>
</html>
and code for view:
<p>Hello, Rails!</p>

Ok, I founded the problem. It was the UTF8 encoding. In Notepad++, You have to choose to encode in UTF8 but without 'BOM', if you choose only UTF8, you get that extra character which add space.
Thanks!

You can use minus sign:
<%= yield -%>
to prevent extra space after

Related

Workaround for #request.getParameters() in Thymeleaf

We are using CAS 5.2.3 which uses an upgraded version of Thymeleaf. Thymeleaf has restricted access to certain request features - '#request.getParameters()' being one. Is there any work around for it? I am getting the following error when trying to access it - "Access to request parameters is forbidden in this context. Note some restrictions apply to variable access. For example, direct access to request parameters is forbidden in preprocessing and unescaped expressions, in TEXT template mode, in fragment insertion specifications and in some specific attribute processors."
Good question. I face this problem a few months before, it is solvable.
After looking through their source code, I found that they are limiting the usage of #request.getParameters() only on specific tag, they didn't forbid to use #request.getParameters() in some situation.
In my use case, I am able to use CData to bypass this checking. Not sure whether it applies to your use case since you didn't provide any code example....
Anyway, the below example want to redirect user to another page, based on the parameter url
Here's an sample code that was broken in CAS 5.2.x, but worked in CAS 5.1.x :
<html>
<head>
<title> Deforestation </title>
</head>
<body th:attr="onload='window.location.href=\''+${#request.getParameter('url')}+'\''">
</body>
</html>
Here's a work around code:
<html>
<head>
<title> Deforestation </title>
</head>
<body>
Logging out. Please wait...
<script th:inline="javascript">
/*<![CDATA[*/
location.href = /*[[( ${#request.getParameter('url')} )]]*/ ;
/*]]>*/
</script>
</body>
</html>
If this didn't solve your problem, please provide your source code so we can have a better look at the problem.
Note: There is a security reason why this stuff is now banned, using this workaround might compromise the security standard, do remember to sanitize the user input if neccesary
Edit:
as per comment, although not elegant, maybe something like the following will work?
<html>
<head>
<title> Data attribute </title>
</head>
<body>
<span id="foobarid"> </span>
<script th:inline="javascript">
/*<![CDATA[*/
$('#foobarid').data('foo-bar',/*[[( ${#request.getParameter('foo') == 'bar'} )]]*/);
/*]]>*/
</script>
</body>
</html>
Apparently one can spend hours, even days on this simple requirement, which Thymeleaf introduced as a poorly dcumented annoyance in later versions. I've spent hours upgrading from an older Thymeleaf version from 5 years ago and got stuck on this very same problem. The lack of documentation didn't either. When I finally took a think break, I've realized the solution to this problem is as simple as using the <c:set> core tag with JSP.
Simply use the th:with tag in some higher level html tag where accessing request parameters is allowed, like this:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml"
th:with="action=${param.action != null ?
param.action[0] : 'none'}">
<head>
...
In this example, the request parameter action is stored in a page context local variable named action (the name can be any unique name that you choose). This variable is now accessible anywhere bellow your high level html tag where you've defined it. It can even be used in following th:xx tags in the same html tag after the declaration. So basically, now you can do this:
<th:block th:switch="${action}">
<title th:case="'edit'" th:text="#{page.contacts.edit}"></title>
<title th:case="'delete'" th:text="#{page.contacts.delete}"></title>
<title th:case="*" th:text="#{page.contacts.add}"></title>
</th:block>
and even call a parameterized fragment by simply referring to your variable as ${action}. In this example I have a navbar template fragment, which accepts three parameters. The last of which, is a request parameter that otherwise, is inaccessible due to the newer Thymeleaf request object access restrictions.
<div id="wrapper">
<div class="nav" th:replace="html/fragments/navbar ::
navbar('html/contact','contact-html', ${action})">
</div>
<div class="content">
...
If you need more request params and more local page context variables, just use a comma to separate the declarations in the th:with tag like this:
th:with="action=${param.action == null ? 'none': param.action[0]},
self=${param.self == null ? 'none': param.self[0]}"
I hope this saves you guys some precious time and voids the frustration of refactoring older Thymeleaf html pages. It definitely beats those horrifically unmanageable CDATA scriptlets inside your pages.

Code Spacing and Indenting Tips

I know this might spur a bit of discussion, and that isn't want this site wants, but this question CAN be answered.
I always code in one way that I am starting to frown upon:
<html>
<head>
</head>
<body>
</body>
</html>
I double space everything, and I NEVER indent, really.
Can anyone advise me on how helpful indenting and such really is? In addition, how should I indent or space if it is better for your code?
Thank you, and...
if this question is not helpful please leave a comment or suggest an edit instead of rating it down...!
EDIT: Note, this edit is after Daniel Imm's answer
How should I Indent stuff like CSS or Javascript or PHP?
Indenting means you can scan/read code much more easily basically. Start doing it and you will not know how you lived without it.
Also when you indent reading non-spaced out code is much easier. Do it like this for HTML, whenever you open a tag, make an indent:
<html>
<head>
<title></title>
</head>
<body>
<div></div>
</body>
</html>

Why does the ASP.NET text shorthand in HTML header not render?

So.. this is the upper part of my ASPX file in an MVC project:
<head runat="server">
<link href="<%= ViewData[SomeNamespace.StyleSheetKey];" %> rel="stylesheet" type="text/css" />
</head>
<div foo="<%= (string) ViewData[SomeNamespace.StyleSheetKey] %>">bar</div>
Now the div tag renders the stylesheet name properly, but the one in the link-tag is rendered as it is written, without being interpreted. In addition a path prefix is added.
So the ASP.NET engine seems to want to hassle with the text in the href-argument in the link tag, "helping" me to prefix my .css file with the correct relative path.
Why? Don't you think I'm able to write the correct path myself?
How will I now be able to set the name of the style sheet programatically?
Well to start
<%= ViewData[SomeNamespace.StyleSheetKey];" %>
needs to be
<%= ViewData[SomeNamespace.StyleSheetKey]; %>"
You got the quotes in the wrong place.

ASP.NET MVC2 Master Page - Server side script not rendering, first bracket being escaped

I have a master page which I am using as a template to allow me to define meta tags per page. My master page takes in a model which contains the meta information, here is an example of what I am trying to do the following:
<meta name="description" content="<%= Model.description %>" />
<meta name="keywords" content="<%= Model.keywords %>" />
However, when I check the HTML once the page is rendered I get this:
<meta name="description" content="<= Model.description %>" />
<meta name="keywords" content="<= Model.keywords %>" />
If I remove the outer quotation marks from the content e.g. content=<%= Model.description %> it renders the data. It doesn't seem to like the surrounding quotation marks.
Is this a bug with the master pages? If so, what would be the best alternative workaround for this? If not, what am I doing wrong?
I have seen that before and it is a pain. Probably you have a runat="sever" attribute in your head tag like this:
<head runat="server">
if you just made it:
<head>
then you should not see this behavior.
This has always been an issue because it is trying to encode the contents in the attributes. You can get around it by doing this instead:
<%= string.Format("<meta name=\"description\" content=\"{0}\" />", Model.description) %>
<%= string.Format("<meta name=\"keywords\" content=\"{0}\" />", Model.keywords) %>
EDIT: This is not an issue specific to MasterPages. I posted a similar question a long time ago here on SO asking about it and if you read the accepted answer you can see that the framework has specific code for various items in the head tag where it will have a slightly different rendering format and will encode the data.
ASP.NET Webform css link getting mangled

Facebook app with an iframe on rails

I'm attempting to create a facebook app and went through the book http://pragprog.com/titles/mmfacer/developing-facebook-platform-applications-with-rails. Everything works fine on my development machine when I have my canvas render method set as fbml. But, for various reasons I want to build an app with an iframe. When I set my app to use an iframe I am unable to connect to my development machine. I went through a lot of different articles about creating an iframe app, but none seem to work. That includes
http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel
http://wiki.developers.facebook.com/index.php/XFBML
and any other site found googling any combination of facebook, iframe, rails and others.
I have the xd_receiver.htm file in my public directory and my layout looks like
<!doctype html public “-//w3c//dtd xhtml 1.0 strict//en” “http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml” xmlns:fb=”http://www.facebook.com/2008/fbml”>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title><%= controller.controller_name %>: <%= controller.action_name %></title>
<%= stylesheet_link_tag "jquery-ui-1.7.1.custom.css", "styles" %>
<%= javascript_include_tag "jquery-1.3.2.min.js", "application" %>
</head>
<body>
<%= yield %>
<script src="http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
<script type="text/javascript">
FB_RequireFeatures(["XFBML"], function(){
FB.Facebook.init("my api key", "xd_receiver.htm"); });
</script>
</body>
</html>
My question is, what am I missing that is preventing facebook from connecting to my rails development machine when I set the canvas render method to iframe? Since my development log does not show any attempts of a connection, I'm guessing that it has something to do with the cross domain stuff, but I can't find a good answer about it anywhere.
If you place that file under /public, its important to note that the ERB contained within the template will not get parsed as you expected. I'm not too familiar with the plugin you are trying to use but have used the Facebooker gem (http://facebooker.rubyforge.org/) which provides a tidy way to interact with the Facebook REST API.
I strongly suggest using Facebooker from the beginning.
It'll save you a lot of time.
/xd_receiver
there are facebooker methods for this sort of thing check this out
http://blog.yangtheman.com/2009/08/23/how-to-integrate-facebook-connect-with-a-rails-app/

Resources