Get Title, Content via Link in Rails

Get title, content via link in rails

Looks like you might be looking for something like Pismo: https://github.com/peterc/pismo

require 'pismo'

# Load a Web page (you could pass an IO object or a string with existing HTML data along, as you prefer)
doc = Pismo::Document.new('http://www.rubyinside.com/cramp-asychronous-event-driven-ruby-web-app-framework-2928.html')

doc.title # => "Cramp: Asychronous Event-Driven Ruby Web App Framework"
doc.author # => "Peter Cooper"
doc.lede # => "Cramp (GitHub repo) is a new, asynchronous evented Web app framework by Pratik Naik of 37signals (and the Rails core team). It's built around Ruby's EventMachine library and was designed to use event-driven I/O throughout - making it ideal for situations where you need to handle a large number of open connections (such as Comet systems or streaming APIs.)"
doc.keywords # => [["cramp", 7], ["controllers", 3], ["app", 3], ["basic", 2], ..., ... ]

An image caveat is:

The image extraction only deals with images with absolute URLs

How to add article title to URL using link_to in Rails app?

Your link_to will be using the route from resources :articles. If you want it to use the custom route you've defined, name the route, and then use that in your link_to:

# routes.rb
match '/articles/:id/:title' => 'articles#show', :as => :article_with_title

# articles/index.html.erb
link_to article.title, article_with_title_path(article, :title => article.title)

Also, you may want to consider looking into using friendly_id. It does this for you, but more transparently.

How to extract my own page title using Rails

The title of a page is specified in its html; therefore it is impossible (ie impractical) to reference the title of a page directly from the page itself and use it to render more content to the page before serving it. You have two options:

1) Use javascript to modify the page after it has been completely rendered. (Not recommended)

2) Modify your rails application so that the title content is accessible anywhere in the view. (Recommended) This stackoverflow answer outlines an elegant way to create a title variable which can be used to set and access the title from anywhere in the view.

Note that in rails, the default title for each page is just the name of your rails app, and it's hardcoded into layouts/application.html.erb. So there really isn't much for you to "capture." If you want your pages to have unique titles, you need to program the back-end for that from scratch, as it were.

Detect links to site’s videos and show title

I did refactoring code and used a gem rinku

def links_in_body(text)
auto_link(text) do |url|
video_id = url.match(/\Ahttps?:\/\/#{request.host_with_port}\/videos\/(\d+)\z/)
if video_id.present?
Video.find(video_id[1]).title
else
truncate(url, length: AppConfig.truncate.length)
end
end
end

Use posts title's for URL

You'll want to call parameterize on the title

def to_param
title.parameterize
end

This will make it a friendly URL with the spaces replaced by dashes. I believe if you don't include the id before title Active Record's find won't work. This would require you to have:

def to_param
"#{id} #{title}".parameterize
end

If you are looking for more versatility check out Friendly ID gem and this railscast:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

Mainly be careful that your routes and calls still work after you make the changes.

Mechanize visit a link and get page title

After clicking on the link, you don't need the block, you're already in a block, iterating over each anchor you found in your "main" URL.

If you click any link, it returns you the page it points to (anchor's href). You can see this inspecting what's on your mechanize variable after that:

page.css(<selector>).each do |link|
mechanize.click(link)
mechanize

=> #<Mechanize
...
#<Mechanize::Page
{url #<URI::HTTPS https://www.theurbanlist.com/brisbane/directory/scout-cafe>}
{meta_refresh}
{title "Scout Cafe, Petrie Terrace | Brisbane | The Urban List"}
{iframes

There you are. Mechanize handles for you now the data belonging to the current page. So now you're able to, through mechanize, using its page method, access its title and all others:

page.css('div[itemprop="articleBody"] ol li a').each do |link|
mechanize.click(link)
puts "Title: #{mechanize.page.title}"
end

Title: Scout Cafe, Petrie Terrace | Brisbane | The Urban List
Title: Southside Tea Room | Brisbane | The Urban List
Title: Spring Hill Deli Cafe, Spring Hill | Brisbane | The Urban List

Notice the use of the itemprop attribute isn't really needed, but I recommend you to add more specific CSS rules/selectors to make the elements easy to recognize.

Using link_to with post.title in Rails?

<h2><%= link_to post.title, post %></h2>

In url, how to show title-name instead of id in ruby on rails?

There is an awesome gem for this purpose named friendly_id, https://github.com/norman/friendly_id

In your Article model you just need to add this,

extend FriendlyId
friendly_id :name, use: :slugged

There are many other options available, for that you need to check the documentation.

Hope that helps!



Related Topics



Leave a reply



Submit