NJF
NJF

Reputation: 435

how to pass different data on different link to same view from same action?

I have events model.I have 3 links as Pastevents,Upcoming events and currentevents.

These 3 links are routed to events_url i.e index action and den to index view.


Following is the code of events controller index action...

def index

      @today = Event.find (:all, :conditions => ['(start_date  = current_date)'], :order => 'start_date ')

      @past  = Event.find (:all, :conditions => ['start_date < ?', current_date], :order => 'start_date')

      @events  = Event.find( :all, :conditions => ['start_date > ?', current_date], :order => 'start_date')

end

I want to pass @today variable data to index view on Currentevents link,@past data on Pastevent link and @event data on upcomingevent link.But i am not able to do pass different variables on respective links.How can i achieve dis?


Following is the code for index view:

- content_for :title do
   Listing Events
 - content_for :div do
   Listing Events
 - content_for :brand_img do
   %img{:src => "images/lifestyle.gif", :height => "35"}
 - @events.each do |event|
   %ol.hoverbox
     %li.increase
       = link_to image_tag(event.photo.url), event_path(event)
       .abc
         = event.name
       %br/
      .bca
         = event.start_date
         |
         = event.start_time
         /|
         /= link_to " ".html_safe, event_path(event), :method => :delete, :class => "del-16", :confirm=>"Are u sure?", :title => "Delete", :style => "text-decoration:none;"

Since in dis view i am referring ti @events variable its showing upcoming events only...How can i change dis variable on different links...

Upvotes: 2

Views: 357

Answers (1)

Agis
Agis

Reputation: 33626

You could use query parameters to do this: You just pass an additional argument to the link_to method like:

<%= link_to "Past events", events_path(view: "past") %>
<%= link_to "Today's events", events_path(view: "today") %>
<%= link_to "All events", events_path %>

Then in your controller you could do something like:

def index
  case params[:view]
  when 'past'
    @past  = Event.find (:all, :conditions => ['start_date < ?', current_date], :order => 'start_date')
  when 'today'
    @today = Event.find (:all, :conditions => ['(start_date  = current_date)'], :order => 'start_date ')
  else
    @events  = Event.find( :all, :conditions => ['start_date > ?', current_date], :order => 'start_date')
  end
end

However, you should also consider following the RESTful approach by adding the corresponding RESTful actions in your controller:

in config/routes.rb:

resources :events do
  collection do
    get 'past'
    get 'today'
  end
end

then in your controller you'd have to define the different actions:

def index
  @events  = Event.find( :all, :conditions => ['start_date > ?', current_date], :order => 'start_date')
end

def past
  @events  = Event.find (:all, :conditions => ['start_date < ?', current_date], :order => 'start_date')
end

def today
  @events = Event.find (:all, :conditions => ['(start_date  = current_date)'], :order => 'start_date ')
end

then in your views:

<%= link_to "Today's events", todays_event_path %>
<%= link_to "Past events", past_event_path %>
<%= link_to "All events", event_path %>

Whatever approach you choose, you should read this guide first: http://guides.rubyonrails.org/routing.html

By the way, you should also consider using named scopes in your model instead of querying it in the controller (so you can keep him thin): http://guides.rubyonrails.org/active_record_querying.html#scopes

This is my first answer here so I wish I could be of help :)

Upvotes: 4

Related Questions