Putting a line break between posts in rails - ruby-on-rails

I'm working with rails and I have gotten a bunch of posts set up. However, I am trying to put a linebreak or some whitespace in between each post. I've got all the articles to display using <%= render #articles %> onto the index page but I am unsure how to proceed to add whitespace. Any help would be greatly appreciated! Thanks!

You should loop trough the articles and add some custom HTML CSS to them.
<% #articles.each do |article| %>
<%= article %>
<%= link_to article.number, article, {:class => "artikel"} %>
<br />
<% end %>
Or add them to a proper html tag like p
<% #articles.each do |article| %>
<p><%= article %></p>
<% end %>

FYI you can also use a second partial that will be rendered by Rails between each instance of your main partial, this can be interesting depending of the complexity of your layout, but probably overkill for just a <br /> ;-)
<%= render #articles, :spacer_template => "article_spacer" %>

Related

Strange output from rails each do

Rails each do method is acting strangely and I do not know why.
controller
def index
#fabric_guides = FabricGuide.with_attached_image.all.order(:name)
end
index.html.erb
<div class="guide-items">
<%= #fabric_guides.each do |fabric| %>
<div class="guide-container">
<%= link_to fabric_guide_path(slug: fabric.slug) do %>
<%= image_tag fabric.image if fabric.image.attached? %>
<% end %>
<div class="guide-info">
<p class="g-name">
<%= link_to fabric.name,
fabric_guide_path(slug: fabric.slug) %>
</p>
</div>
</div>
<% end %>
</div>
I have two FabricGuide records so I expect two "guide-container" but I get three. Or more precisely I get two guide containers and a third block of text containing all the content from the last FabricGuide record.
I have almost an identical setup for articles and have never encountered this problem. I'd happily share more information if needed. Thank you!
Please remove = equal sign from your each loop of view code
like below :-
<% #fabric_guides.each do |fabric| %>
...
...
<% end %>
you have used this <%= #fabric_guides.each do |fabric| %> in your view that's why it shows all record in DOM.
The expression for erb tags is <% %>
now if we want to print that tag too then we apply <%= %>

Ruby on Rails - Underline words if they appear in dynamically generated text using Ruby

I am trying to underline words that are dynamically generated by the debug(params) method provided by rails. I have something below, but it obviously does not work, plus what I have below is attempt to try and change the words using methods that I already know about (like the .upcase method). I was hoping to underline the word controller if it appears in the text using only Ruby. Can anyone help me out here?
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<%= 'controller'.upcase %>
<% end %>
thanks
edit:
I should add that debug(params) is a method defined by RAILS, I was able to do the following which seems even more off, so far the answers have not been correct to what I want to do.
<% if Rails.env.development? %>
<% debug_method = debug(params).split.each do |word| %>
<% if word == 'controller:' %>
<ul><% word.upcase %></ul>
<% end %>
<% end %>
<%= debug_method.join %>
<% end %>
which returns the following text: https://ibb.co/cvnEpw , keep the answers coming in though. I want to get the words in the original box (that's generated by the method to underline the controller word https://ibb.co/jmSm2G).
use <u></u> tag
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<u><%= 'controller'.upcase %></u>
<% end %>
example here
Provide the css to generate html element:
p { text-decoration: underline; }
Add html elemnt to wrap your words:
<%= debug(params) if Rails.env.development? %>
<% if debug(params).include?('controller:') %>
<p> <%= 'controller'.upcase %> </p>
<% end %>
The answer to the question is below. I had to use the .gsub and .html_safe methods.
<%= debug(params).gsub("controller:", "<u>controller:</u>").html_safe %>
This code keeps the existing html & css generated by rails intact

List of articles also displays database info for all articles in View

I am doing the Jumpstart tutorial where you have to create a blog in ruby on rails. It is going fine but I have not managed to figure out one thing.
The root page is supposed to show a list of articles. And it does. Only it also shows this extra bit:
Article list and unwanted part at the bottom
The code I have in the view:
<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 New Article", new_article_path, class: 'new_article' %>
And here is the relevant code in the controller
def index
#articles = Article.all
end
I would appreciate any help on why this is happening.
Take out the = sign of your loop
So:
<% #articles.each do |article| %>
In Embedded Ruby (ERB), the difference between <% %> and <%= %> is not simply cosmetic; the latter actually prints the results of the line of code into the html. As such, in your line:
<%= #articles.each do |article| %>
...You are actually printing the results of running that line of code and displaying it. Or, more specifically, you're displaying loop itself, not just the results of looping through the #articles collection. You'll get a lot of gibberish from haphazardly using the <%= %> notation. Just look at when you play around in the console/debugger; running these lines of code makes a lot of noise!
To fix your problem, your loop through #articles should simply look like this:
# No "=" here!
<% #articles.each do |article| %>
<li>
<%= link_to article.title, article_path(article), class: 'article_title' %>
</li>
<% end %>
For more information on ERB syntax, look here.
For another stack overflow question encompassing this and a few additional notation, look here.
<% #articles.each do |article| %>
<%= content_tag :li, link_to(article.title, article, class: 'article_title') %>
<% end %>
Struggled with the same issue for 2 days when starting.

Rendering issue with <dc:creator> tag from Wordpress RSS2

I'm having issues with rendering the content of the XML tag from my Wordpress RSS feed. Here's my code to display the last 2 posts which I draw in from the controller:
<h3>Check out our latest blog posts:</h3>
<% unless #latest_blog_posts.nil? %>
<% #latest_blog_posts.each do |post| %>
<% if nil != post && post.respond_to?(:pubDate) %>
<h4><%= link_to post.title, post.link, :target => "_blank" %>
(by: <%= post.dc:creator %> - <%= time_ago_in_words(post.pubDate) %> ago)</h4>
<%= (post.description).slice!(0, 195).html_safe %>[...]
<% end %>
<% end %>
<% else %>
<p>Woops, looks like there's no posts to show. Sorry about that.</p>
<% end %>
The tag gives an error due to the ":" in the tag. I've tried using another variable and rendering the contains in a string:
article_author = '#{post.dc:creator}'
That renders "#{post.dc:creator}" in the view (I thought it would but I gave it a try anyway). Does anyone have a solution to this? Thanks.
Might as well answer my own question since it's the first result in a relative Google search. Hopefully it can help someone out in the future. The xml node was being parsed as:
dc_creator
<%= post.dc_creator %>
Thanks to everyone that checked out my question and tried to help.

Iterative display of objects in Rails

I find that when I iteratively render a collection of objects, say, comments, rails also lists the addresses of the objects. For example, my view may contain the following:
<h3>Comments</h3>
<% if #blogpost.comments.any? %>
<%= #blogpost.comments.each do |comment| %>
<%= render :partial => "comment", :locals => {:comment => comment} %>
<% end %>
<% end %>
The view often shows this:
<h3>Comments</h3>
<p>comment #2</p>
<p class="post-info"> >> Example User, about 1 hour ago. </p>
<p>this is user 1's comment on user 5's article</p>
<p class="post-info"> >> Example User, 2 days ago. </p>
#<Comment:0xb6f91968>#<Comment:0xb6f9016c>
As you can see, there are a couple of address listings for the objects, which I would prefer not to have in the view. Is there a way to suppress this output? Thanks in advance for your time!
remove = from <%= #blogpost.comments.each do |comment| %>
when we add = in <% %> rails will execute erb code and render the output in html.
Also, you don't really need all of this code.
You can left only these two lines to render 'comment' partial for #blogpost.comments collection:
<h3>Comments</h3>
<%= render #blogpost.comments %>
and Rails will do the trick.
http://api.rubyonrails.org/classes/ActionView/Partials.html

Resources