My Rails 3.0.3 has a “month” that has a link where the user can upload the image using “save to." Now I need to create an association in which the model of the month belongs to the wallpaper model.
Routes
root :to => 'inicio#index' resources :wallpapers do resources :months end
Model of the month:
class Month < ActiveRecord::Base belongs_to :wallpaper has_attached_file :wallpaper_picture, :styles => { :default => { :geometry => '530x330', :quality => 80, :format => 'jpg'} } end
Friendlyid Wallpaper Model:
class Wallpaper < ActiveRecord::Base has_many :months, :dependent => :destroy extend FriendlyId friendly_id :title, :use => :slugged end
In months_controller, I created a loading method, this method works without 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 tried changing the days_controller method to load, but this is wrong:
@months = Wallpaper.month.conditions({:person_id => some_id})
ruby-on-rails-3
Ricardo castañeda
source share