eRB and multiple statements syntax? - html

I have the following in an html.erb file, using link_to to get a hyperlink and t() to internationalize my text. But it looks very clunky:
<p><%= t('session.new_user') %><%= link_to(t('session.signup_now'), signup_path) %></p>
Splitting onto multiple lines seems wrong since the text will all appear on the same line on screen but is there a better syntax to avoid the two consecutive <%= %> blocks?

I would probably go for line breaks:
<p>
<%= t('session.new_user') %>
<%= link_to t('session.signup_now'), signup_path %>
</p>
or you could set variables before the actual code
<% new_user_text = t('session.new_user') %>
<% link = link_to t('session.signup_now'), signup_path %>
<p><%= new_user_text %><%= link %></p>
or you could set instance variables in the controller. I wouldn't like that for view stuff like this.
Extra: if you like tidy code you may like haml
%p
= t('session.new_user')
= link_to t('session.signup_now'), signup_path
now it is actualle readable!

You can add a hyphen before the closing tag to prevent a newline being appended to the output.
<% ... -%>
Please note that this feature is Rails specific.

Related

Print multiple lines in a text_area_tag in rails

I have a loop for comments that I want to print in a text_area_tag. Here is my code
<%=text_area_tag 'body', nil, rows: 10, cols: 25%>
<% #comments.each do |comment|%>
<p><%=comment.user_name%> says: <%=comment.comment%></p>
<%end%>
What I am trying to do is, instead of printing out all the comments in the p tags, I want to print them inside the text_area_tag
You can use the Rails helpers like simple_format
<% #comments.each do |comment| %>
<p><%=comment.user_name%> says: <%= simple_format comment.comment %></p>
<% end %>
Other helpers you may be wanting to experiment with: (text = comment.comment)
<%= raw text %>
<%= h text %>
<%= text.html_safe %>

Remove unexpected content on a website

While following a tutorial on building a Ruby-on-Rails blogging website, I'm running into some unexpected results. The project so far is stored on https://github.com/khpeek/jumpstart-blogger.
The main page is an "Articles" page, which looks like this:
So far, so good (except for the somewhat curious position of the "Create a New Article" button, which used to be directly below the articles).
The appearance of "All Articles" is governed by app/views/articles/index.html.erb, which reads
<h1>All Articles</h1>
<ul id="articles">
<% #articles.each do |article| %>
<li>
<%= link_to article.title, article_path(article), class: 'article_title' %>
</li>
<% end %>
</ul>
<%= link_to "Create a New Article", new_article_path, class: "new_article" %>
The h1 heading is the first thing in the .html.erb file, and also the first thing that appears on the web page.
However, if I click on an article link, say "Article with Ruby Tag", I see the page below:
Besides the desired box with the article, tags, and comments, there are also two submit buttons and "<< Back to Articles List" buttons which are neither desired nor expected.
The appearance of this page is governed, as I understand it, by app/views/articles/show.html.erb, which reads
<h1><%= #article.title %></h1>
<p>
Tags:
<% #article.tags.each do |tag| %>
<%= link_to tag.name, tag_path(tag) %>
<% end %>
</p>
<% if #article.image.exists? %>
<p><%= image_tag #article.image.url %></p>
<% end %>
<p><%= #article.body %></p>
<h3>Comments (<%= #article.comments.size %>)</h3>
<%= render partial: 'articles/comment', collection: #article.comments %>
<%= render partial: 'comments/form' %>
<%= link_to "<< Back to Articles List", articles_path %>
<% if logged_in? %>
<%= link_to "delete", article_path(#article), method: :delete, data: {confirm: "Really delete the article?"} %>
<%= link_to "edit", edit_article_path(#article) %>
<% end %>
The first line in this file is the h1 header, but the 'unexpected' contents seems to come before that. So I'm having trouble seeing where to start to remove this content. Any pointers?
You're dealing with layout Rails' concept. Read this.
Anyway, you probably have a layout file in app/views/layouts.
check your application.html.erb in layouts folder.. it is rendering in header on some condition

Submitting forms in Rails - Paragraph breaks are converted into spaces

I am trying to create articles for my blog. I got the controller working correctly, except for a spacing issue.
In my text-area field, when I type in paragraphs and hit return the paragraphs are morphed into one paragraph.
This is what the output looks like when I view the source code
<li><strong>jksdf</strong></li></br>
<li>This is the first sentence.
This is the second sentence.
This is the third sentence.</li>
When I view it in development, it looks like this
This is the first sentence. This is the second sentence. This is the third sentence.
Stack Overflow managed to get this to work in their app, so why can't I?
Here is my Index page
<ul class = 'articles-list'>
<% #article.each do |article| %>
<li><strong><%= article.title %></strong></li></br>
<li><%= article.body %></li>
<% if signed_in? && current_user.admin? %>
<li><%= link_to 'Edit', edit_article_path(#article)%></li>
<li><%= link_to 'Delete', article, method: :delete%></li>
<% end %>
<% end %>
</ul>
My New Articles page with the form
<h3>New Article</h3>
<div class = 'center'>
<%= form_for #article do |f| %>
<p>
<%= f.label :title, class: 'marker' %>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body, class: 'marker' %>
<%= f.text_area :body, :rows => "15" %>
</p>
<p>
<%= f.submit 'Submit', :class => 'btn btn-success' %>
</p>
<% end %>
</div>
My Migration File
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
I don't see why it's rendering a space instead of a paragraph break.
Do I need to include linebreaks in my text box? I can't use gsub, that won't work either.
Edit: It might have to do with using the li html tag. Maybe that forces all text to one line.
Edit: No that doesn't work either. I tried replacing ul and li tags with div tags, and I still received the same error.
I've tried the following
Using Line Breaks in the text-area box
Replacing UL and LI tags with DIV tags
Try using simple_format on the displayed text:
<%= simple_format article.body %>
It should convert single line breaks into <br> tags and wrap paragraphs (bordered by double line breaks) in <p> tags. HTML essentially ignores line breaks which is why you need some markup inserted.
If you need additional formatting options you'll probably want to look at integrating markdown (or something similar) in your application using something like Redcarpet.

How to not show a line break when blank?

In my Ruby app I'm using the following code in my view:
<% if post.image.present? %>
<%= image_tag post.image_url(:thumb).to_s %>
<% else %>
<%= "" %>
<% end %>
If there's no image then it shows a blank which works fine.
The problem is that I do not want to show a line-break. Instead of <%= "" %>, is there something I can use to do that?
I'd do it like this. Instead of a massive if/else statement, use the Ruby one-liner, and instead of an empty string, don't add anything if it's null:
<%= image_tag post.image_url(:thumb).to_s if post.image.present? %>
Note: If it's still adding a line-break, that's probaby something in your CSS. Nothing about this code should give you a line break.

Inserting rails into HTML file

When writing an HTML file, why use <%= INSERT RAILS HERE %> vs. <% INSERT RAILS HERE %>
<%= %> emits a string, <% %> runs code.
On the pedantic side, you're writing an ERb template, not an HTML file--the syntax is the same whether it's a template for HTML, JS, or whatever.
The ERB docs provide additional (but not complete) information.
<%= %> will return value and display in your page. Assume that you have person.name = 'Dark'
<%= person.name %>
will display Dark in your web page.
<% %> will not return any value to your page. It just embed simple ruby code. Usually used with `control statement'.
<% if person.present? %>
<span><%= person.name %></span>
<% end %>
When we use <%= %> it simply displays the value returned, on the html page.
<% %> executed the code but doesn't dispaly it on the html page.