How to use twitter REST api along with railscast 241 - ruby-on-rails

REST noob here:
I'm using the code from ryan bates rails cast #241
http://railscasts.com/episodes/241-simple-omniauth
I can authenticate with my user just fine....
Now I want to display the last tweet I made on the views/articles/index.html.erb page....
I want to just want to display the data from this REST api thing..."https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline"
How do I change this code in the most simplest way?
<% title "Articles" %>
<div id="articles">
<!-- TODO grab my tweets with GET Statuses/home_timeline -->
<% for article in #articles %>
<h2>
<%= link_to article.name, article %>
<span class="comments">(<%= pluralize(article.comments.size, 'comment') %>)</span>
</h2>
<div class="created_at">on <%= article.created_at.strftime('%b %d, %Y') %></div>
<div class="content"><%= simple_format(article.content) %></div>
<% end %>
</div>
<p><%= link_to "New Article", new_article_path %></p>
Thank you for help.

You should take a loot at twitter gem: http://sferik.github.com/twitter/
You can grab a user's timeline like: timeline = Twitter.user_timeline( twitter_username )

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 <%= %>

I have called an image from Active Storage with url_for. Is there any way, to link that image with the post that belong to? in rails

i have this call for the image from Active Storage, using url_for
<% #posts.each do |post| %>
<div class="card-group card-group-size">
<div class="card recipes recipes_posts">
<img src="<%= url_for(post.image) if post.image.attached? %> " class="index_images">
<div class="card-body">
<h5 class="card-title"><%=link_to post.title, post, class: "post_title"%></h5>
</div>
</div>
</div>
<% end %>
This was the only way to call it there (/post/index.html.erb).
I could call the same image with <%= image_tag #post.image, class: "image_view" %> in posts/show.html.erb, but this wont work on any other page.
i dont have any issues with the loading of the image, that work as is should, i only wish to know if i can make onclick that image to link to the post that belongs.
thank you
Wrap your image in link tag passing link to show action. I assume there is a routes configure and available as helper post_path
<%= link_to post_path(post) do %>
<%= image_tag post.image, class="index_images" if post.image.attached? %>
<% end %>

Using mail_to with a block (mail_to ... do)

I would like to do the following to insert a styled mail_to:
<%= mail_to #colleague.email do %>
<span class="email" id="colleague_40"> Reply by Email </span>
<% end %>
The RoR docs aren't specific on the topic of mail_to blocks but the above code doesn't seem to work. Is there a way to do a block in conjunction with mail_to?
<% mail_content = capture do %>
<span class="email" id="colleague_40"> Reply by Email </span>
<% end %>
<%= mail_to #colleague.email, mail_content %>

How to generate a proper hyperlink by leveraging link_to()?

<h3>Social Media Handles: (<%=link_to 'Seel All Social Media', influencer_influencer_social_media_handles_path(#influencer)%>)</h3>
<ul>
<% #influencer.influencer_social_media_handles.each {|social_media_handles| %>
<li><strong>Social Media Name:</strong> <%=social_media_handles.social_media_name%><br />
<%=link_to(social_media_handles.social_media_handle) %>
</li>
<% } %>
</ul>
This <%=link_to(social_media_handles.social_media_handle) %> is actually a link to a twitter page i.e http://twitter.com/#!/SrBachchan. But when I click on it nothing happens. How can I make this link working so it will take me to a twitter page.
Thanks
Please take a look here for a comprehensive usage of link_to tag in rails.
<%= link_to 'Twitter Profile', social_media_handles.social_media_handle %>
The method is link_to(:body, :url, :options)
So, you should:
<%= link_to 'Twitter Profile', social_media_handles.social_media_handle %>
Assuming that social_media_handles.social_media_handle returns "http://twitter.com/#!/SrBachchan" that will generate:
Twitter Profile

How do I wrap link_to around some html ruby code?

How do I wrap a link around view code? I can't figure out how to pass multiple lines with ruby code to a single link_to method. The result I am looking for is that you click the column and get the show page:
<div class="subcolumns">
<div class="c25l">
<div class="subcl">
<%= image_tag album.photo.media.url(:thumb), :class => "image" rescue nil %>
</div>
</div>
<div class="c75r">
<div class="subcr">
<p><%= album.created_at %></p>
<%= link_to h(album.title), album %>
<p><%= album.created_at %></p>
<p><%= album.photo_count %></p>
</div>
</div>
</div>
link_to takes a block of code ( >= Rails 2.2) which it will use as the body of the tag.
So, you do
<%= link_to(#album) do %>
html-code-here
<% end %>
But I'm quite sure that to nest a div inside a a tag is not valid HTML.
EDIT: Added = character per Amin Ariana's comment below.
Also, this may be an issue for some:
Make sure to write <%= if you are doing a simple link with code in it instead of <%.
e.g.
<%= link_to 'some_controller_name/some_get_request' do %>
Hello World
<% end %>
For older Rails versions, you can use
<% content_tag(:a, :href => foo_path) do %>
<span>Foo</span>
<% end %>
You can use link_to with a block:
<% link_to(#album) do %>
<!-- insert html etc here -->
<% end %>
A bit of a lag on this reply I know -- but I was directed here today, and didn't find a good answer. The following should work:
<% link_to raw(html here), #album %>

Resources