Thursday, November 26, 2015

How to create a valid RSS feed in Rails

6:28 PM Posted by Unknown 1 comment

Step 1

Create a new method in the posts controller.
app/controllers/blog_articles_controller.rb
class PostsController < ApplicationController

  ...

  def feed
    @posts = Post.all.order('created_at DESC').limit(20)
    respond_to do |format|
      format.rss { render :layout => false }
    end
  end

end

Step 2

Add a "view" for the feed.

Create the file
app/views/posts/feed.rss.builder
xml.instruct!
 
xml.rss :version => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom' do
 
  xml.channel do
 
    xml.title 'Feed title'
    xml.description 'Feed description'
    xml.link root_url
    xml.language 'en'
    xml.tag! 'atom:link', :rel => 'self', :type => 'application/rss+xml', :href => feed_url
 
    for post in @posts
      xml.item do
        xml.title post.title
        xml.link post_url(post)
        xml.pubDate(post.created_at.rfc2822)
        xml.guid post_url(post)
        xml.description post.description
      end
    end
 
  end
 
end

Step 3

Add a route to /feed in the router file.

Edit
config/routes.rb

Add
get 'feed' => 'posts#feed'

Step 4

Add the following entry in the head section of your site template.

Edit
views/layouts/application.html.erb

Add a line like
<link href="https://www.your_website.com/feed?format=rss" rel="alternate" title="Your website RSS Feed" type="application/rss+xml"></link>
Then you can check Feed Validator for Atom and RSS here:  http://feedvalidator.org

Sunday, November 22, 2015

How to install Solr 5.3.1 on Ubuntu 14.04

7:25 PM Posted by Unknown No comments

Step 1: Installing Java

Solr requires Java, so we will install it if you didn't install it before

First, use apt-get to install python-software-properties:

sudo apt-get install python-software-properties

Install the latest version of Java 8 instead of using the default-jdk or default-jre packages

sudo add-apt-repository ppa:webupd8team/java

You will need to press ENTER to accept adding the repository to your index.

Update the source list and install Java8:
sudo apt-get update 
sudo apt-get install oracle-java8-installer

Step 2: Installing Solr

We will begin by downloading the Sorl distribution
wget http://apache.mirror1.spango.com/lucene/solr/5.3.1/solr-5.3.1.tgz

Extract the service installation file: 

tar xzf solr-5.3.1.tgz solr-5.3.1/bin/install_solr_service.sh --strip-components=2 

Install Solr as a service using the script: 

bash ./install_solr_service.sh solr-5.3.1.tgz

Check if the server is running: 

sudo service solr status

Step 3: Creating a Collection

Solr canhave mutiple collections, so you can create a collection by browser at this address: http://localhost:8983/solr/#/~cores or by command:

sudo su - solr -c "/opt/solr/bin/solr create -c collection_name -n data_driven_schema_configs"
Note: Options of solr service
/etc/init.d/solr {start|stop|restart|status} 
Or
sudo service solr  {start|stop|restart|status}