Reputation: 3345
I am following the Rails guide for doing multi-model nested form. I have 2 models, Page and Picture. Page has_many Pictures. I'm putting the Picture file field within the Edit Page form using fields_for.
Each time I upload an image, the form will add an additional file field to allow for a new Picture to be uploaded.
The behavior I want is for the Page form to only always have one file field which will create a new Picture. I don't need previous pictures to be editable.
The questions thus are 1) how can I do the above? 2) should I even be using nested form? Because I'm not editing other parts of the Page when creating a picture.
Upvotes: 0
Views: 146
Reputation: 4187
You can just use:
#routes.rb
resources pages do
resources pictures
end
#PicturesController.new
@picture = Picture.new
#views/pictures/new.haml
= form_for @picture
#form code here
Or you can place form wherever you want and send it to pictures controller:
#views/pages/show.haml
= form_for [@page, Picture.new] do |f|
= f.hidden_field :page_id, :value => @page.id
= f.file_field :file #change to your own
= f.button :submit
#PicturesController
def create
@picture = Picture.new(params[:picture])
if @picture.save
redirect_to :back, :notice => "success"
else
#some code
end
end
Upvotes: 1