How to set virtual attributes while saving multiple models?

i need to let the user choose a few optional parameters through
checkboxes, that when one is checked for example, will do this on the
Upload model:

@thumbs.update :Small => ‘600x800>’

and if another checkbox is checked, may do this

@thumbs.update :Large => ‘1280x1024>’

here is my form

new.rhtml

Upload Photo

<% form_for(:photo, :url => {:action => "create_upload"}, :html =>{ :multipart => true }) do |form| %>

Title:
<%= form.text_field :title %>

Image:
<% fields_for "photo[upload_attributes][]", @photo.uploads[0] do |data_form| -%> <%= data_form.file_field :uploaded_data %> <% end -%>

<%= submit_tag "Upload Photo" %>

<% end %>

here are my models and controllers

photo.rb

class Photo < ActiveRecord::Base

has_many :uploads
belongs_to :user

def upload_attributes=(upload_attributes)
upload_attributes.each do |attributes|
uploads.build(attributes)
end
end
end

upload.rb

class Upload < ActiveRecord::Base
belongs_to :photo

@thumbs = { :thumb => ‘124x143>’ }

has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 5000.kilobytes,
:thumbnails => @thumbs

validates_as_attachment

end

photos_controller.rb

def new
@photo = Photo.new
@photo.uploads.build
end

def create
@photo = Photo.new(params[:photo])
@photo.user_id = @user.id

if @photo.save
  flash[:notice] = 'Photo was successfully uploaded.'
  redirect_to :action => :gallery
else
  flash[:notice] = 'Photo failed to upload.'
  render :action => :upload
end

end

can you please tell me how to add these key=>values to the @thumbs so a
user can choose what sized images will be generated through a form like
this:

new.rhtml

Upload Photo

<% form_for(:photo, :url => {:action => "create_upload"}, :html => { :multipart => true }) do |form| %>

Title:
<%= form.text_field :title %>

<p>
  Image:<br />
  <% fields_for "photo[upload_attributes][]", @photo.uploads[0] do

|data_form| -%>
<%= data_form.file_field :uploaded_data %>

<% end -%>
  <ul>
    <li><%= form.check_box :XSmall %></li>
    <li><%= form.check_box :Small %></li>
    <li><%= form.check_box :Medium %></li>
    <li><%= form.check_box :Large %></li>
  </ul>
</p>

<p>
  <%= submit_tag "Upload Photo" %>
</p>

<% end %>

or maybe this

#new.rhtml

Upload Photo

<% form_for(:photo, :url => {:action => "create_upload"}, :html => { :multipart => true }) do |form| %>

Title:
<%= form.text_field :title %>

<p>
  Image:<br />
  <% fields_for "photo[upload_attributes][]", @photo.uploads[0] do

|data_form| -%>
<%= data_form.file_field :uploaded_data %>


  • <%= data_form.check_box :XSmall %>

  • <%= data_form.check_box :Small %>

  • <%= data_form.check_box :Medium %>

  • <%= data_form.check_box :Large %>


<% end -%>
</p>

<p>
  <%= submit_tag "Upload Photo" %>
</p>

<% end %>

any help would be appreciated, thanks in advance.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs