我的 Rails 3.0.3 应用程序有一个脚手架“月”,其中有一个链接,用户可以使用“保存到”下载图像。现在我需要将月份模型属于_壁纸模型的位置关联起来。
路线:
root :to => 'inicio#index'
resources :wallpapers do
resources :months
end
# the route that works with no association
# match 'download/:id' => 'months#download', :as => :download
# the route I tried
match 'wallpapers/:id/months/:id' => 'months#download', :as => :download
月份型号:
class Month < ActiveRecord::Base
belongs_to :wallpaper
has_attached_file :wallpaper_picture, :styles => {
:default => { :geometry => '530x330', :quality => 80, :format => 'jpg'}
}
end
带有友好ID的壁纸模型:
class Wallpaper < ActiveRecord::Base
has_many :months, :dependent => :destroy
extend FriendlyId
friendly_id :title, :use => :slugged
end
在months_controller中我做了下载方法,这个方法没有关联:
class MonthsController < InheritedResources::Base
belongs_to :wallpaper, :finder => :find_by_slug!
def download
@wallpaper = Wallpaper.find(params[:wallpaper_id])
@month = @wallpaper.month.find(params[:id])
send_file @month.wallpaper_picture.path,
:filename => @month.wallpaper_picture_file_name,
:type => @month.wallpaper_picture_content_type,
:disposition => 'attachment'
end
end
查看月份/索引
- @months.each do |month|
= link_to image_tag(month.wallpaper_picture(:default)), wallpaper_month_path(month.wallpaper, month)
我已经尝试在months_controller 中更改下载方法,但它是错误的:
@months = Wallpaper.month.conditions({:person_id => some_id})