Rails:带有自定义路由的数据库记录......?

时间:2021-04-29 22:15:38

I have a model, target, that holds a number of records that are timestamped. On the corresponding controller, I list the months of those records by doing the following:

我有一个模型,目标,它包含许多带时间戳的记录。在相应的控制器上,我通过执行以下操作列出这些记录的月份:

In models/target.rb

def month
   self.recorded_on.strftime('%B')
end

In controllers/targets_controller.rb

@records = Target.find :all

In views/targets/index.html.haml

%ul
  - @records.group_by(&:month).sort.each do |month, data|
    %li= link_to month, ''

That all works great for listing the available months for the records that I have. Next, I want to be able to click on the month and get a report of all the records for that month, at the following path generated with year and the month: /targets/2009/04

这一切都非常适合列出我所拥有的记录的可用月份。接下来,我希望能够点击月份并获得该月所有记录的报告,在以下年度和月份生成的路径中:/ targets / 2009/04

How would I do this?

我该怎么办?

1 个解决方案

#1


Add some named scopes to your Target model to support finding by year and by month number. Something like:

将一些命名范围添加到Target模型,以支持按年份和月份编号查找。就像是:

class Target < ActiveRecord::Base
  named_scope :by_month,
    lambda { |month| { :conditions => ['MONTH(recorded_on) = ?',
                        month] }}

  named_scope :by_year,
    lambda { |year| { :conditions => ['YEAR(recorded_on) = ?', year] }} 
  .
  .
  .
end

(Note that the conditions here are using MySQL syntax.)

(请注意,这里的条件是使用MySQL语法。)

Assuming you're using RESTful routes, set up a named route like the one below in your config/routes.rb file (make sure it's declared before the default route):

假设您正在使用RESTful路由,请在config / routes.rb文件中设置如下所示的命名路由(确保在默认路由之前声明它):

map.targets_by_month '/targets/:year/:month', :controller => 'targets',
                :requirements => { :year => /\d{4}/, :month => /\d{1,2}/ },
                :conditions => { :method => :get }

—You can use this route in your view like this:

- 你可以在你的视图中使用这条路线,如下所示:

<%= link_to 'Show April 2009 Targets', targets_by_month_path('2009', '04') %>

(Note that the leading zero for the month is optional because of the :requirements regular expression in the named route defined above)

(请注意,月份的前导零是可选的,因为:上面定义的命名路径中的:需要正则表达式)

Finally, in your TargetsController, set up the index action to use the named_scopes defined earlier:

最后,在TargetsController中,设置索引操作以使用之前定义的named_scope:

def index
  @records = Target.by_year(params[:year]).by_month(params[:month])
  .
  .
  .
end

#1


Add some named scopes to your Target model to support finding by year and by month number. Something like:

将一些命名范围添加到Target模型,以支持按年份和月份编号查找。就像是:

class Target < ActiveRecord::Base
  named_scope :by_month,
    lambda { |month| { :conditions => ['MONTH(recorded_on) = ?',
                        month] }}

  named_scope :by_year,
    lambda { |year| { :conditions => ['YEAR(recorded_on) = ?', year] }} 
  .
  .
  .
end

(Note that the conditions here are using MySQL syntax.)

(请注意,这里的条件是使用MySQL语法。)

Assuming you're using RESTful routes, set up a named route like the one below in your config/routes.rb file (make sure it's declared before the default route):

假设您正在使用RESTful路由,请在config / routes.rb文件中设置如下所示的命名路由(确保在默认路由之前声明它):

map.targets_by_month '/targets/:year/:month', :controller => 'targets',
                :requirements => { :year => /\d{4}/, :month => /\d{1,2}/ },
                :conditions => { :method => :get }

—You can use this route in your view like this:

- 你可以在你的视图中使用这条路线,如下所示:

<%= link_to 'Show April 2009 Targets', targets_by_month_path('2009', '04') %>

(Note that the leading zero for the month is optional because of the :requirements regular expression in the named route defined above)

(请注意,月份的前导零是可选的,因为:上面定义的命名路径中的:需要正则表达式)

Finally, in your TargetsController, set up the index action to use the named_scopes defined earlier:

最后,在TargetsController中,设置索引操作以使用之前定义的named_scope:

def index
  @records = Target.by_year(params[:year]).by_month(params[:month])
  .
  .
  .
end