Can I connect a named object to a link in Rails?

I have an event model that has two namespaces:

class Event < ActiveRecord::Base
  scope :future, lambda { where('events.date >= ?', Time.zone.now) }
  scope :past, lambda { where('events.date <= ?', Time.zone.now) }
end

I call these areas from my controller, creating two new controller actions (called "future" and "past"):

My controller:

class EventsController < InheritedResources::Base
  actions :index, :show, :future, :past

  has_scope :future
  has_scope :past

  def future
    index! { @events = Event.future }
  end

  def past
    index! { @events = Event.past }
  end
end

I also have views and routes for these additional activities.

It works great. My application does what I want, and I can refer to the new actions in the sidebar as follows:

%li
  = link_to 'Events Home', events_path
%li
  = link_to 'Future Events', future_events_path
%li
  = link_to 'Past Events' past_events_path

, , , , , . , . , , , . , , .

, "" "" :

%li
  = link_to 'Events Home', events_path
%li
  = link_to 'Future Events', events_path.future
%li
  = link_to 'Past Events' events_path.past

, . , select_tag, , JS...

+3
2

. has_scope gem

+3

, :

Event.send(params[:scope] : 'all')

:

link_to "Future events", events_path(:scope => "future")
+5

Source: https://habr.com/ru/post/1763735/


All Articles