I have a project with the following relationships:
class Project < ActiveRecord::Base
has_one :photo
has_one :project_detail
class Photo < ActiveRecord::Base
belongs_to :project, :accessible => true, :creator => true
class ProjectDetail < ActiveRecord::Base
belongs_to :project, :accessible => true, :creator => true
I am trying to create a page with 4 forms, 3 to upload photos with
paperclip, and one to upload project_details.
The page is created by the ProjectsController. In that I currently
have
been creating the following in ‘new’:
@project = Project.new
@project.build_photo
@project.photo.save (If I save project, does it also save
project.photo?)
@project.photo_id = @project.photo.id (does rails do this
automatically?)
@project.build_project_detail
@project.project_detail.save
@project.project_detail_id = @project.project_detail.id (does
rails do this automatically?)
@project.save
redirect_to :action => “edit”, :id => @project.id, :photo_id =>
@project.photo_id, :project_detail_id => @project.project_detail_id
The above seems very redundant and unnecessary. I would like to know a
much better way of going about
this. The reason for saving and redirecting to edit was so that the
project id would be created when I upload
a photo.
The page is supposed to function by being able to upload the 3 images,
and then upload the project_details.
Since AJAX does not support file uploads, I am going to try and
implement an ‘Ajaxy’ solution using iframes and the
“responds_to_parent” plugin. However, first I would like to know
answers to the following:
-
What is a better approach than the one above for setting up the new
project page? -
In my photo form I am passing @project.photo. This does get handled
by the photo controller, but the
following does not work in the PhotosController:@project = Project.find(params[:project_id])
@project.photo.update_attributes(params[:photo])A hidden field in the form is:
The above does not work.
So what is the correct way to achieve the above?
This does work: @photo = Photo.new(params[:photo]) …but is not
what I want.
The photo model has fields for 3 photos, so I need to use the same
record for all
3 uploads.
-
In the photos controller, what is the correct way to redirect to
the page I just came from.In the Projects controller I used:
redirect_to :action => “edit”, :id => @project.id, :photo_id =>
@project.photo_id, :project_detail_id => @project.project_detail_id…which worked great. I thought I could just change that to
"projects/edit, but no. Something like
:url => (page I just came from)??
Thanks,
Jet
Oh, and a bonus question. If I have the above mentioned relationship
with project and photo,
and I create an instance in the projects controller: @project.photo
how would I access the photo data in the returned params hash from a
form,
e.g. to access the project data (params[:project])
would accessing photo part be (params[:project => :photo])
…and an individual element (params[:project => :photo
=> :name]) ???