Rails 4; How to Display an RSS Feed on HTML View? - html

I'm building a personal website using Ruby with Rails 4.
I'm attempting to scrape Medium.com and have my Medium articles show up on my page. It's almost working, they are displaying on my page how I would want them, but it also displays the entire RSS code underneath my blog posts. I can't figure out how to get this data dump off my view.
What can I do to change this?
Here is my code on the controller:
def blog
require 'rubygems'
require 'rss'
require 'open-uri'
#rss = RSS::Parser.parse('https://medium.com/feed/#AlextheYounger/')
end
Here is the code on my HTML view:
<%= #rss.items.each do |item|%>
<div class="row">
<div id="article-div" class="col-xs-8">
<div class= "well well-lg">
<div class= "article-title">
<p><%="#{item.title}"%></p>
</div>
</div>
</div>
</div>
<% end %>
Again, it actually does display my blog posts, it just does so with a massive data dump included. How do I get rid of this massive data dump?

The issue is that <%= differs from <%
The equals sign tells rails "spit out the result of the current ruby statement onto the screen into your html"... in this case, you are spitting out the result of the ruby-code: #rss.items.each... which happens to return the value of #rss.items (which is all of your rss feed).
For ruby-statements that don't need to output to the screen (like this), just change to using the non-equals-sign version: <% and you should be good to go.

Related

Is there a way to add HTML inside a Rails partial argument?

I have been trying to find something about embedding HTML code inside a partial argument for days but I have not found anything so I'm guessing it isn't possible. But it seems like it should be.
I have a static page in my Rails app which has a lot of sections and each section can have subsections. I could just make the entire page just plain HTML. But I didn't want to repeat the same formatting over and over in case I want to change classes or something else.
So I have the following _section.html.erb partial file:
<div class="row">
<h4><%= heading %></h4>
<% subsections.each do |section| %>
<% if section[:header] %>
<h5 class="primary-text"><%= section[:header] %></h5>
<% end %>
<p><%= section[:body] %>
<% end %>
</div>
That works fine. But what if I want to include a link to a page or an email inside one of the subsections? It doesn't work just by passing it in as part of the quotes text. It shows the actual HTML tags.
Is there a real way to do this or should I give up and just write plain HTML with repeated section formatting?
You mark your text as html_safe. For example:
<%= section[:header].html_safe %>
But I would suggest using sanitize method because of security resonons:
<%= sanitize section[:header] %>
Probably sometimes you will want to configure sanitize method. Here you can read how to do this:
http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
You can read more about security here:
http://guides.rubyonrails.org/security.html#cross-site-scripting-xss

Embedding a rails view within a view

Rails noob here. I'm attempting to create create single-page scrolling site similar to this ...but, you know, not nearly as attractive :-)
Anyway, I've got my javascript working however I've developed much of the content (contact form, about us page etc etc) in different views which are hanging out in various html.erb files. Rather than cut and paste the contents of each into a single home.html.erb, I was wondering if there was a clean way to just embed the content from each view into my homepage view. Something like this:
<h1>My awesome homepage!</h1>
<div>
<div id="about">
<%= put_page_here page=about_us %>
</div>
<div id="our_product">
<%= put_page_here page=about_us %>
</div>
<div id="contact">
<%= put_page_here page=contact %>
</div>
</div>
If you would recommend going about this entirely differently, please let me know.
Many thanks in advance!
This should work:
<%= render partial:"shared/contact_us", locals:{variable:value} %>
Note that it will look for a view in app/views/shared/_contact_us.html.erb - notice the _ before the view. Pass the variables the view needs in through locals. More information on passing variables around here.

Ruby on Rails - Masthead div in application.html.erb, render in all pages except homepage?

I tried reading guides.rubyonrails but I cant seem to figure this out:
I have a masthead in my application.html.erb file in my rails 4.0 application.
I want the masthead div to be rendered in all pages except the homepage.
Thanks for your help!
You can wrap it in a conditional ruby statement like
<% unless current_page?(root_url) %>
<div>Your html code</div>
<% end %>

Photos I upload to my ruby-on-rails server look bad

showing bad quality image on ruby on rails websit
ruby script:
<div align="center">
<p align="center">
<%= if property.images[0] then link_to image_tag
(property.images[0].image.url('700x525', :jpg)),
user_property_image_path(user, :id => property.images[0],
:property_id => property) end -%>
</p>
</div>
hot to improve the photo quality?
The loaded photo is 600x400px, I thought the website was displaying it as 533x354px, but it's not actually scaling it, it's just showing what's (probably) inside your div, and leaves the overflow invisible.
The photos I can see are actually low quality, are those the original ones? (If so, there's not much to do here, unfortunately!)

How to target html elements when logged into DNN

Silly question and I hope I just overlooked this. In Sitefinity, once you are logged in, there is a wrapping div which one can use to target certain html tags ONLY when you are logged in.
I am not seeing anything like this in DNN and was wondering if someone knows of a solution to this please?
What I would like, is something like this:
[div class="dnn-logged-in"]
[div class="my-content-wrapper"]
[div class="elements-i-would-like-to-show-or-hide]
Hide or show this if I am logged in or not
[/div]
[/div]
[/div]
Many thanks!
I'd say this is a fairly uncommon task because this type of logic is generally handled on the server side and not the client side. This is because you generally don't want to increase the client payload any more than you need to - and if the user isn't logged in - they'll never need this content. In addition there are obviously security implications if working with sensitive data - as the information is easily viewable in the underlying markup.
All disclaimers aside, If you do want to do this, I think the best solution would be to just make site work just like you're used to in Sitefinity. In DNN you would create a container that contains the appropriate logic. A container wraps each module, and within the container you have the opportunity to write any code (server or client) to make your container dynamic.
For this brute force example I modified the "Invisible.ascx" container that ships with the DarkKnight skin in DNN 6:
<%# Control language="C#" AutoEventWireup="false" Inherits="DotNetNuke.UI.Containers.Container" %>
<%
var isLoggedIn = HttpContext.Current.User.Identity.IsAuthenticated;
if (isLoggedIn)
{
%>
<div class="dnn-logged-in">
<% } %>
<div class="Invisible"><div id="ContentPane" runat="server"></div></div>
<% if (isLoggedIn) { %>
</div>
<% } %>
This will yield the following HTML when not authenticated:
<div class="DnnModule DnnModule-DNN_HTML DnnModule-364">
<a name="364"></a>
<div class="Invisible">
<div id="dnn_ctr364_ContentPane">
[Dynamic Content Here]
</div>
</div>
</div>
and this when you are authenticated:
<div class="DnnModule DnnModule-DNN_HTML DnnModule-364">
<a name="364"></a>
<div class="dnn-logged-in">
<div class="Invisible">
<div id="dnn_ctr364_ContentPane">
[Dynamic Content Here]
</div>
</div>
</div>
</div>
If you want this logic everywhere, I'd just enhance each of the containers that you're currently using with this type of logic.
You can use SkinObject to do so in dnn. Login skin object will help you showing a Login/ Logout OR Regiter, User Skin object will help you show the currently logged in user. You can use any style and div you want.
In DotNetNuke this is controlled by permissions, which can be assigned to pages or to individual modules (specific content). By default, new pages will have VIEW permissions for "All Users", and new content created on the page will inherit that permission.
To change this, select the settings for the specific content, un-check the "Inherit View Permissions from Page. This will enable the check-boxes for all the various roles. Then simply ensure that All Users is un-selected, and instead select "Registered Users". This will make the content visibleto any user who is logged into the site.
This is a super powerful permission model which actually controls the rendering, rather than just obscuring visibility.