GET request and Fancybox working with rails - ruby-on-rails-3

I'm pretty sure theoretically what I am trying to do should work and I believe my syntax is correct. But it would be good to get some fresh eyes on it.
I want to pull in articles from a site (GET request) and display those articles in a fancybox, so my users wouldn't need to leave the site. What am I missing here?
Thanks for your help on this.
<ul class="box_qa">
<% #hero_news.each do |item| %>
<li>
<%= item["webTitle"] %> <%= link_to "Read more!", item["webUrl"], :class=>"fancybox" %>
<span class="timestamp">
Posted <%= time_ago_in_words(item["webPublicationDate"])%> ago.
</span>
</li>
<% end %>
</ul>
<script>
$(document).ready(function(){
$("a.fancybox").fancybox({'type': 'image'});
});
</script>

Switched "image" to iframe. Stil having sizing issues however.

Related

Adding menus in Rails 3

A bit of issue here. Im trying to add a menu to all pages. Reason for this is the ease of editing a single file which updates all web pages.
In my layouts/application.html.erb I have this, between body tags:
<% content_for :menu do %>
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<% end %>
<%= yield %>
And in my welcome/index I have:
<div id="menu">
<%= yield :menu%>
</div>
<h1>Welcome to my index page!</h1>
Not sure if all that is needed so when I go to my root, I only see what is in the welcome/index file and not the links. Am I missing something?
Its should be other way around, that means you could call the :yield in application layout and have the content_for in your index file.
Actually the idea of the content_for tags are to allow slightly different variations for different pages but, still calling the same name from layout. read more about content_for from here
And I think , in your case what you need is a partial in the layout, or even you can have your menu in the layout itself. since the layout is visible for every page your menu will be available for each page, and if you need modifications in later, still you have to change only one page
1 - as a partial
#app/views/layouts/_menu.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
#app/views/layouts/application.html.erb
<%= render partial: 'menu' %>
<%= yield %>
2 - having all in the same file
#app/views/layouts/application.html.erb
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
<%= yield %>
you should have the <%= yield :menu%> in your application.html file.
Also you can make a folder (lets name it "shared") where you can have your menus, messages etc.
Then you can try something like this
#welcome/index.html.erb
<div class="content">
Your Index Content here.
</div>
#layouts/application.html.erb
<%= yield :menu %>
<%= yield %>
#shared/menu.html.erb
<% content_for :menu do %>
<div id="menu">
<ul>
<li> page 1 </li>
<li> page 2 </li>
</ul>
</div>
<% end %>

link to the current_user via href

Currently I have a header that links the user to his profile through this:
<li><%= link_to '<i class="icon-picture"></i> My Profile'.html_safe, current_user %></li>
I would like to be able to link the current user to his profile by doing something like this:
<li>
<a href="<%= current_user %>">
<div><%= image_tag current_user.avatar (:small) %></div>
<strong>Surge Pedroza</strong><br/>
view my profile page
</a>
</li>
But I do not know how to do it through an <a href="...">. Can someone please help me out?
This would be a better way to accomplish what you want:
<%= link_to current_user do %>
<div><%= image_tag current_user.avatar(:small) %></div>
<strong><%= current_user.full_name %></strong><br/>
view my profile page
<% end %>
link_to .. do is useful for the exact situation you are describing. Check the documentation for more examples.
I actually got it :)
<a href="<%= current_user.id %>">

RoR: how can I paginate all of my users microposts all in one place?

I want the home page to show everyones microposts, but I keep getting errors. I feel like this might be because the microposts have a belong_to has_many relationship with users. But anyways, This is the code for the home page..
<section>
<%= render 'shared/user_info' %>
</section>
<section>
<div id= "purchases">
<%= render 'shared/micropost_form_purchase' %>
</div>
<div id="sales">
<%= render 'shared/micropost_form_sale' %>
</div>
</section>
<ol class="microposts">
<%= render #microposts %>
</ol>
<%= will_paginate #microposts %>
and it gives me this error: 'nil' is not an ActiveModel-compatible object that returns a valid partial path. at the bottom.
I added
def home
#microposts = Micropost.all
end
to the microposts controller.
can anyone help me out? please?
The call to render is looking for a partial view that it can't find. If you're keeping all your partials in views/shared/, then do you have a views/shared/micropost.html.erb for it to render?

Conditional link display in rails

I have a filter bar on my page. The bar should always be in place, however only when I'm on the detail page I want to show a link <- back to list inside of it. Otherwise the filter bar should be empty. What is the most elegant way of doing this in rails 3 or 3.1?
Thanks
Thomas
To return to previous page you can use link_to "Back", :back
To show or hide the link you can use the controller_name and action_name methods with a if/unless conditional.
From your question and the comment, you have the following structure:
application.html.erb:
...
<section id="filter-bar">
<section id="filter"></section>
</section>
I see there two different options how to include your link conditionally:
By doing an if-then in your file application.html.erb
By including a yield with a symbol that denotes the context.
Here is the pseudo-code for that:
solution
application.html.erb:
...
<section id="filter-bar">
<section id="filter">
<% if controller_name == 'user' && action_name == 'show' %>
<%= link_to "Back", :index %>
<% end %>
</section>
</section>
solution
application.html.erb:
...
<section id="filter-bar">
<section id="filter">
<%= yield(:filter) %>
</section>
</section>
view.html.erb:
<%- content_for :filter do %>
<%= link_to "Back", :index %>
<% end %>
...
index.html.erb:
// No content_for section in the file, so it will be empty here.
The first solution is simpler, much more condensed, but all the information if something is included or not is in one file. If that is changed a lot, that may be a hotspot in your application. The second is more object-oriented, but perhaps more to change and think about. But both will working for you.

Weird rendering of .html.erb file

So I have this file:
<h1>Calendar view</h1>
<div class="events">
<% #events.each do |e| %>
<%= raw(e.content)%>
<% end %>
</div>
<br />
<div class="messages">
<% #messages.each do |m| %>
<%= raw(m.content)%>
<% end %>
</div>
With #events and #messages being valid instance variables in the controller...but when I go to the page the html looks like this:
<h1>Calendar view</h1>
<div class="events">
<br>
<div class="messages">
This is another message test
</div
Event Content
</div>
I'm confused. Maybe I'm missing something obvious?
The problem is that raw() will output raw HTML content. The Rails template engine will try to merge that with the .erb template you supplied.
Therefore, if either m.content or e.content are malformed, you will most likely get unexpected output.
The best way would be to look for syntax errors, especially missing closing elements.