Wednesday 27 August 2014

What is the use :anchor in link_to rails

<li class=""><%= link_to "International Events",events_events_filter_path(:id=>1,:anchor => 'critic-review'), :data=>{:toggle=>"tab"}%>
HtmlL:
<a data-toggle="tab" href="/events/1#citic-review">International Events</a>
it mewns it will execute a method and pont to some div or some place with id="rcitic-review"

Monday 25 August 2014

Dealing With routes


Define post method:

post '/listing/:id(.:format)', to: 'events#listing', :as=>"filter"

run rake routes

it will generate like this............

 events_filter POST   /events/listing/:id(.:format)                events/events#listing

in ur view:

<li><%= link_to "Latest Events",events_filter_path(:id=>"latest"), method: :post %></li>

This will generate: http url as:

http://localhost:3000/events/listing/latest

In ur latest method of events controller u can access as id:

def latest
   id=params[:id]
end





Thursday 21 August 2014

HOW TO SAVE IMAGES USING Mongoid::Paperclip

Mongoid::Paperclip

The ‘mongoid-paperclip’ gem is specially used in rails application with mongodb database where as the ‘paperclip’ gem is use in rails application with other than mongodb database like mysql, postgreSQL etc.

Before start this tutorial make sure that the ‘mongoid’ gem is already installed in your application. Lets follow following steps:
1. Include the gem in your Gemfile
gem "mongoid-paperclip", :require => "mongoid_paperclip"
2. Install gem (run on command prompt):
bundle install
3. Create controller (say Image ):
rails g controller Images index new show
4. Create model (say Image):
rails g model Image
5. Add following code in app/models/image.rb
class Image
include Mongoid::Document
include Mongoid::Paperclip
has_mongoid_attached_file :image
validates_attachment_content_type :banner_image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
end
6. Add following code in images controller.
def new
@images=Image.new
end
6. Use following code to create form (in views/Images/new.html.erb)
<%= form_for(@images, :url => {:action => 'create'},     :html => {:multipart => true}) do |f| %>
<%= f.file_field :image %>
<%= f.submit "Upload" %>
<% end %>
7. Use following code to Upload image (in ‘create’ action of  controllers/image_controller.rb)
def create
@image = Image.new(images_params)
if @image.save
redirect_to :action => :show, :id => @image.id
end
end
8. Use following code to view uploaded image (in ‘show’ action of controllers/image_controller.rb)
@id = params[:id]
@image = Image.find(@id)
9. and add in (views/image/show.html.erb)
<%= image_tag @image.image.url %>
10. Start server and run on browser:
localhost:3000/images/new
11. your images will saved in /public/system/images/image..
If you want see path of image..
in your terminal..
rails c
Image.last.image.url(:default)
12. if you want to add styles to your image..like specific width...use below code in your model
has_mongoid_attached_file :banner_image, :styles => { :default => "598x246>"}
13.Help Ful links:
https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation


U are using same image with different sizes in same project u can define three styles as follows:

has_mongoid_attached_file :image, :styles => { :small=> '124x89',:medium=> '247x173',:large=> '598x299'}
Then u can access image as following way:
<%= image_tag @image.image.url(:large) %> #for large image
<%= image_tag @image.image.url(:small) %> #for small image
<%= image_tag @image.image.url(:medium) %> #for medium image

If u make any changes..need to apply total images in ur table just simpl run below command:
 rake paperclip:refresh CLASS=Events::Photo Happy Learning.......


Tuesday 5 August 2014

Date and Month in single select box rails...last six months and last one year...


in controller:

@date = Date.today
@months = []
(0..17).each do |m|
@months << [@date.prev_month(m).strftime("%b, %Y"), @date.prev_month(m)]
end

In ERB:

<%= select_tag "month_year", options_for_select(@months) %>


In Order to get next 6 months and next year months:

@date = Date.today
@months = []
(0..17).each do |m|
  @months << [@date.next_month(m).strftime("%b %Y"), @date.next_month(m)]
end