Submitting forms in Rails - Paragraph breaks are converted into spaces - html

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.

Related

Display image thumbs uploaded with PaperClip in a grid

I am trying to display six images uploaded with PaperClip in a grid type fashion.
Whatever I try, the images only display in one line. If I change the class to gallery it instead shows one image per line.
I have also tried #each_slice with no luck, and using index was my last attempt.
index.html.erb
<div class="page-header"><h1>My Work</h1></div>
<div class="media">
<% #documents.in_groups_of(3, false).each_with_index do |document_group, index| %>
<% document_group.each do |document| %>
<div class="media-left">
<% if index < 3 %>
<%= link_to image_tag(document.doc.url, class: 'media-object', size:"108x152"), document.doc.url, target: '_blank' %>
<%= link_to 'Remove', document_path(document), class: 'btn btn-danger', method: :delete, data: {confirm: 'Are you sure?'} %>
</div>
<div class="media-body">
<h4 class="media-heading"><%= document.title %></h4>
</div>
<% end %>
<% end %>
<% end %>
</div>
</div>
documents_controller.rb
class DocumentsController < ApplicationController
def index
#documents = Document.order('created_at')
end
end
This isn't really a problem to be solved with Ruby. Are your thumbs a fixed width? If so, you could put them in a div of 3x{thumb width + whatever margins, padding is needed}, allowing room for only three on any line.
Or, you could use nth child selectors maybe?

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

eRB and multiple statements syntax?

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.

Writing HTML code with Rails

<% #ticket.conversations.each do |c| %>
<section class="messages">
<%="<li> #{c.the_message} </li>" %>
</section>
<%end%>
I am trying to have rails write the HTML code for me so the output would look something like this:
<li>MESSAGE1</li>
<li>MESSAGE2</li>
<li>Next message here...</li>
I am going to style every nth element to have a different style to show what speaker it belongs to. But currently is just outputs straight text and escapes the HTML. How do I stop this escape?
To output you need to use <%= as follows within your <section> block:
<%= "<li> #{c.the_message} </li>".html_safe %>
But currently is just outputs straight text and escapes the HTML
You can use the html_safe method. Please refer to the "Extensions to String" topic in this document: http://guides.rubyonrails.org/active_support_core_extensions.html
Another option you can use is the raw helper(as pointed out by Stefan) which calls the html_safe for you. e.g.
<%= raw "<li> #{c.the_message} </li>" %>
You can also style your list items this way:
<li><%= c.the_message %></li>
Just based upon preference.
Try it this way:
<% #ticket.conversations.each do |c| %>
<section class="messages">
<li><%= c.the_message %></li>
</section>
<% end %>
Or if you don't want to repeat <section> every time:
<section class="messages">
<% #ticket.conversations.each do |c| %>
<li><%= c.the_message %></li>
<% end %>
</section>