How to make a link with many elements in rails using haml - ruby-on-rails

I am very new in ruby, in rails and in haml. I was recoding a little site I have, and I was trying to create a link in wich is nested a few elements.
The structure I want to be outputted is this one:
<a href="somewhere">
<span>
<img src="/imgs/hello.png" />
</span>
<strong>6
<em>Oct</em>
</strong>
<h1>Title</h1>
</a>
So I was trying it doing this (obviously its real indentation is one tab more than the parent element):
= link_to("somewhere") do
%span
%img(src="/imgs/hello.png")
%strong 6
%em Oct
%h1 Title
end
Wich for me seems to be logic. But it does not. It gives me this error:
Inconsistent indentation: " \t\t\t\t\t" was used for indentation, but the rest of the document was indented using 1 tab.
The error line number it's the first line after link_to; the %span element. So, I thought it was because I was not using link_to in a correct way, but seeing that the error talks about indentation problems and after trying it in many different ways, I cannot get it into run.

This should work.
= link_to("somewhere") do
%span
%img(src="/imgs/hello.png")
%strong
6
%em Oct
%h1 Title
Also, make sure in your editor you're using spaces as tabs. This will get rid of any funky business.

Related

How can I load a div in rails in response to clicks on another div?

I'm very new to Rails (and web) programming, so I'm not even sure what technology I should be looking for for this.
I've downloaded and run through the first five chapters of the Rails tutorial, but now have a very simple request.
On the left hand side of a web page, I will have a table. If the user clicks on an element in that table, I want to have the right hand side of the page show something new.
I already have a page to display the table, viz:
<div class="center hero-unit">
<div class="container">
<h2>2012 Yearly Report</h2>
<div class="row-fluid">
<div class="span12">
<div class="span4">
<table border="1">
</table>
</div>
<div class="span6">
<!-- load stuff here based on what someone clicks on in the table -->
</div>
</div>
</div>
</div>
</div>
And I'm using bootstrap layouts to display everything. I just don't understand how to change the contents of the 'span6' div based on user behavior in 'span4'.
This is a difficult question to answer. It really depends on what kind of data you're trying to display and what sort of interactivity you're looking for.
You don't really provide much information about what you're trying to accomplish, but if I had to guess, you're trying to load data from your database and insert it into an element without leaving the current page. This is what AJAX is for (your tutorial goes into it a bit in chapter 11) and involves a good deal of javascript, which is generally beyond the scope of a server side language like Ruby. Luckily, rails includes helpers for making it easy to include AJAX features into your web application without having to write a lot of javascript (although you'll have to write some).
As an example, suppose your table has a list of articles, and you want to display the contents of an article in a div when its link is clicked on.
First the link:
<%= link_to article.name, article_url(article), :remote => true %>
The remote option tells Rails that it's an AJAX link.
Next, you need to render a javascript template for your article's show action. You'll name it show.js.erb.
Supposing the div you want the data to be loaded into looks like this,
<div id='article-content'></div>
you'll want your show.js.erb to contain the following:
$('#article-content').html("<%=javascript_escape #article.content %>");
This javascript (with embedded ruby) code will be evaluated when one of your remote links is clicked and will replace the content of your div with the article's content.
There is plenty of resources online to give you more information. It looks like railscasts just released an episode on this topic just a week ago. It requires a subscription to view, but is well worth it (especially if you're just starting out).

How to get the same view output of text that i should enter in my text box?

i am typing some text in my textbox like below format....:-
eg...
(hi i am xyz.
i work in a company which is product based.
and i work as a software engineer.)
after typing above text however i am pressing submit i am getting the below result in my view:-
(hi i am xyz.i work in a company which is product based.and i work as a software engineer.)
I am not getting how to get the display same as i have written in the textbox.
I will be highly thankful if someone help me.
Try putting it inside a pre tag.
<pre><%= #variable_name %></pre>
Or try simple_format
<%= simple_format(#variable_name) %>
Look into the simple_format helper:
my_text = "Here is some basic text...\n...with a line break."
simple_format(my_text)
# => "<p>Here is some basic text...\n<br />...with a line break.</p>"
You can also add the option to warp it with specific tags or perform sanitization.

IE7 and forms in tables (forms inside tables causing extra line breaks in IE7 only)

Rails 2.3.5 (internal work server - stuck at this version for inside apps)
The company 'standard' browser where I work is IE and about 70% of users are using IE7.
What I've slowly been learning (IE7 only) is that if you have FORM beginning or end tags inside a TR or TD, IE7 will create extra lines, sometimes doing very odd things. My solution so far is to put FORM beginning and end tags outside the TABLE tags.
Then, because I want a single line break between tables ... if I use a tag after tables in IE7 only I'll get 3 blank lines between tables where in every other browser there will just be a single line.
Right now I'm dealing with a simple table list of users with a form on each line (delete or change access level). After playing around A LOT with this, IE7 messes up the least when I place the FORM and FORM END tags inbetween table tags like:
<table class="table_standard_blue">
<tr>
<td>
FOO
</td>
</tr>
<% users.each do |user| %>
<% form_for(user) do |f| %>
<tr>
<td>
SOME SELECT / SOME BUTTON
</td>
</tr>
<% end %>
<% end %>
</table>
While the 'guts' of the table will look fine this way, the problem this leaves behind is basically what looks like an extra line break above and below the table (in IE7 only). If I have a couple of tables like this, the effect magnifies and it looks like two blank lines between tables (where in IE8/Firefox) there will be no blank lines.
I know there's something about RAILS putting extra spaces in with FORM tags (and there's suppose to be some fix in RAILS 3 - which I can't use of course at work). Does anyone have any idea how I could fix or hide what's going on in IE7?
Thanks - much appreciated.
I'm not sure you're allowed to place form tags around tr elements, but to answer your question:
Use CSS to reduce the margin-top and margin-bottom of the tables. Before that, though, are you using the right DOCTYPE declaration? That's important to make sure IE7 isn't falling back to emulate layout quirks from IE6 or something.
Before you try what Satya suggested you can try to wrap the form into a span-tag; not sure if it works here but worth a try.
Thanks for the help. Span tags didn't have any affect at all. Also, I went throught multiple doc types and none of them mand any difference. I kept google searching and found a solution though. I'm not 100% sure but it seems to be a problem with IE7 and forms in general (not something a RAILS form_for is doing). Adding this into my stylesheet fixed everything:
FORM
{
display: inline;
}

how to create a strong element inside a p

In haml, how do I render the following incredibly basic HTML:
<p>Results found for <strong>search term</strong>
where 'search term' is actually a Ruby variable called #query?
I'm trying the following,
%p results found for <strong>= #query</strong>
But that renders = #query literally. If I try:
%p results found for <strong>
= #query
</strong>
then the query term renders correctly, but is on a new line.
Also, I'm wondering if there's a better way to render <strong> in haml, while keeping everything on the same line.
I'm aware of the haml documentation, but as far as I can see there isn't an example of using a simple inline Ruby variable.
-----UPDATE-------
The following code works, and shows how to use a variable that's not within tags:
%p
= #trials_found_count
results found for
%strong= #query
But I find it really unreadable - it's hard to tell that it renders as just one line of HTML without adding a comment above.
Is there a way I can put all this code on a single line? Or is this just how haml works?
HAML is whitespace delimited. Nested tags go on the line below and one level in from the tag above. Embedded Ruby from which you want to display output is opened with an '='. Embedded Ruby which you don't want to display such as the start of loops uses '-' These are equivalent to <%= %> and <% %> respectively in erb.
What you want would look like this:
%p
results found for
%strong= #query
Which would produce the html:
<p>results found for <strong>#query</strong></p>
It should be noted that the '=' to start Ruby evaluation can only come at the beginning of the line or after a tag declaration and that only one tag declaration can occur per line.
The Ruby Evaluation section of the reference you linked covers embedded Ruby in detail and the haml tutorial which covers embedded ruby and many other haml basics is here:
http://haml-lang.com/tutorial.html
Here's how I'd do it:
%p No results found for <strong>#{h #query}</strong>
I'm not sure, but you might need a non-breaking space to preserve the space between the for and the <strong>
%p results found for
%strong= #query

Correct coding convention for embedded code on web page templates

I had come experience with PHP a time ago and now I'm learning to use Ruby on Rails. But one simple question bothered me in both these languages, so I think I can cross-post it to both tags.
As you know, one of the concepts there is that one can embed PHP or Ruby code into web page template. Then these statements are executed and result of its execution is inserted in certain places of the page, marked with "brackets" <%= ... %>.
Or... wait. We program Ruby/PHP, but not HTML. Maybe we should treat template as Ruby/PHP code, into which sometimes HTML markup is inserted? So the process is treated like that HTML are inserted into ruby code into the "brackets" %> ... <%.
These are two different approaches:
HTML page is the primary entity, and it is affected by code execution; or
code is the primary entity, and it is executed, while HTML snippets are inserted in certain places.
This philosophy leads to different possibilities in coding conventions: result of code execution influences the page If we adhere the first insight, then the code would look like this:
<p>
<% (1..10).foreach do |i| %>
Iteration number <strong><%= i %></strong>. <br/>
<% end %>
</p>
But if we stick to the second alternative, the code would be formatted like this:
<%
%><p><%
(1..10).foreach do |i|
%>Iteration number <strong><%
%><%= i %><%
%></strong>. <br/><%
end
%>
How should the concept of templates be construed? What concepts do you, way more experienced Web developers, account for the coding convention?
If this is in your View layer (and it should be), then the HTML is the primary entity. It's the most pertinent part of that layer -- marking up your data to display in meaningful ways to the user.
Even aside from that, your second example is nearly unreadable. I see what you're doing, but it took me a minute to wrap my brain around it. I've also never, ever seen View-layer code like your second example (and I would make it one of my priorities to change it wherever I saw it if it was in a project I was working on).
To be more concise: you're putting the emphasis on the wrong thing. In my opinion, readability trumps just about everything else. The coding style that produces the most readable code is therefore the most superior (ceteris paribus and YMMV, of course).
Maybe you should look into Haml? I don't know if there's a php equivalent, but as far as Rails goes, it's somewhere in between the two schemes. It's not quite code centric. But when used right, all the raw html is prepared programatically.
In short everything is considered text to be directly outputted, unless prefixed with either a %, - or =. Which translate to html-tag, ruby code that doesn't output. Ruby code that does output. Haml then uses whitespacing to nest things properly, much like python does. Raw html outputs untouched but using % to specify a tag handles closing tags.
Sample:
#outer-div
- #items.each do |i|
%span.item
= i
%br
Outputs
<div id="outer-div">
<span class="item">
item
</span>
<br>
</div>
See the haml tutorial for more information.
To answer the central question. The bulk of any page is going to be HTML or raw text. We reduce the bulk of that text with includes and helpers, but it's still there. If there were a truly code centered approach my use of it would depend on the ratio of program logic to html. Personally I'd rather go with the html centered approach.
If you are interested in a code-oriented view, this is something you might try implementing as a pure Ruby DSL:
tag :p, :class => 'iterations-container' do
(1..10).each do |i|
text "Iteration number "
tag :strong { text i }
text "."
tag :br
end
end
Or perhaps instead of tag :p do ... end, you may favor tag.p do ... end.
I recommend doing only very simple logic in your template files. That way designers who can edit HTML can easily edit even those files to alter the layout if need be.

Resources