theRemix ..
2007-Dec-13 07:02 UTC
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
<h1>Upload Photo</h1>
<% form_for(:photo, :url => {:action => "create_upload"},
:html =>{
:multipart => true }) do |form| %>
<p>
Title:
<br />
<%= form.text_field :title %>
</p>
<p>
Image:
<br />
<% fields_for "photo[upload_attributes][]",
@photo.uploads[0] do
|data_form| -%>
<%= data_form.file_field :uploaded_data %>
<% end -%>
</p>
<p>
<%= submit_tag "Upload Photo" %>
</p>
<% 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
<h1>Upload Photo</h1>
<% form_for(:photo, :url => {:action => "create_upload"},
:html => {
:multipart => true }) do |form| %>
<p>
Title:<br />
<%= form.text_field :title %>
</p>
<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
<h1>Upload Photo</h1>
<% form_for(:photo, :url => {:action => "create_upload"},
:html => {
:multipart => true }) do |form| %>
<p>
Title:<br />
<%= form.text_field :title %>
</p>
<p>
Image:<br />
<% fields_for "photo[upload_attributes][]", @photo.uploads[0]
do
|data_form| -%>
<%= data_form.file_field :uploaded_data %>
<ul>
<li><%= data_form.check_box :XSmall %></li>
<li><%= data_form.check_box :Small %></li>
<li><%= data_form.check_box :Medium %></li>
<li><%= data_form.check_box :Large %></li>
</ul>
<% end -%>
</p>
<p>
<%= submit_tag "Upload Photo" %>
</p>
<% end %>
any help would be appreciated, thanks in advance.
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---