0

My Rails 3.0.3 application has a scaffold 'month' which has a link where the user can download an image using 'save to'. Now I need to make an association where the month model belongs_to the wallpaper model.

Routes:

  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

Month model:

class Month < ActiveRecord::Base
  belongs_to :wallpaper

  has_attached_file :wallpaper_picture, :styles => {
    :default => { :geometry => '530x330', :quality => 80, :format => 'jpg'}
  }
end

Wallpaper model with friendlyid:

class Wallpaper < ActiveRecord::Base  
  has_many :months, :dependent => :destroy

  extend FriendlyId
  friendly_id :title, :use => :slugged
end

In months_controller I made the download method, this method works with no association:

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

View months/index

- @months.each do |month|  
  = link_to image_tag(month.wallpaper_picture(:default)), wallpaper_month_path(month.wallpaper, month)

I've tried changing in the months_controller the download method, but it is wrong:

@months = Wallpaper.month.conditions({:person_id => some_id})
Ricardo Castañeda
  • 5,746
  • 6
  • 28
  • 41

1 Answers1

0

Here is how I got it
Routes

resources :wallpapers do  
  resources :months  
end  
match 'wallpaper/:wallpaper_id/download/:id' => 'months#download', :as => :download

In the routes I must pass the :wallpaper_id (has_many :months),
the :id is the id of the current controller (belongs_to :wallpaper)
'download' will be the name of the path used in the view 'download_path'
In this path I must pass the foreign key and the id

View months/index

- @months.each do |month|     
  = link_to 'Download Picture', download_path(month.wallpaper_id, month.id) 

In months_controller the download method will receive those parameters and pass the associated image to the send_file method.

def download
  @wallpaper = Wallpaper.find(params[:wallpaper_id])
  @month = @wallpaper.months.find(params[:id])

  send_file @month.wallpaper_picture.path,
              :filename => @month.wallpaper_picture_file_name,
              :type => @month.wallpaper_picture_content_type,
              :disposition => 'attachment'
end

PD: if send_file fails in Production, change it to send_data or
comment out this line in config/production.rb

config.action_dispatch.x_sendfile_header = "X-Sendfile"  

send_file just sends an empty file

Community
  • 1
  • 1
Ricardo Castañeda
  • 5,746
  • 6
  • 28
  • 41