I have a nested form for a user, portfolios, and photos. Basically a user can create a portfolio by uploading photos in ANOTHER FORM. However, then I want to give them a chance to create a new portfolio, by selecting some photos from the current portfolio they are viewing, and have the method resubmit in PortfolioController create a new portfolio for them. The models are:
class User < ActiveRecord::Base
has_many: portfolios
end
class Portfolio < ActiveRecord::Base
has_many: photos
belongs_to: user
end
class Photo < ActiveRecord::Base
belongs_to: portfolio
attr_accessor :move
end
The controller is:
class PortfolioController < ApplicationController
//... some generic code
def resubmit
// This is where I need help
end
def display
@userPortfolio = Portfolio.where(:id => params[:uid]).first
end
end
and the view is:
<%= simple_form_for @userPortfolio, :url => {:action => resubmit} do |f|%>
<%= f.label current_user.name %>
<% @images = @userPortfolio.photos %>
<% @images.each do |anImage| %>
<%= f.fields_for :anImage do |ff| %>
<%= ff.checkbox :move, :id => "move_#{anImage.id}" %><%=ff.label :move, "Move image #{anImage.name} to new portfolio?" %>
<% end %>
<% end %>
<%= f.submit "Create new portfolio" %>
<% end %>
Basically once the user hits submit I want the method resubmit to create a new portfolio with a collection of new photos that are the same as the photos selected. That is, I want to create 1 new record of portfolio and several new records photo, as many as the user has selected, based on the properties of the photos the user selected, so I need to access the records that represent the selected photos. How do I access all the photos the user has selected? I can't simply create a finite set of checkbox controllers on the form since the number displayed depends on the number of photos in the current portfolio.