My models are
class History < ActiveRecord::Base
belongs_to :projects_lkp
has_many :pictures
accepts_nested_attributes_for :pictures
end
class Picture < ActiveRecord::Base
belongs_to :history
validates_presence_of :pics
end
history/new.html.erb
<%= form_for(:history, :url => {:action => 'create'}) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<% end %>
history/_form.html.erb
<%= d.fields_for :pictures do |dd| %>
<div class="row form-group"></div>
<div><class='col-md-5 form-label'>Photo</div>
<%= dd.file_field :pic, multiple: true, :class => 'col-md-15' %>
</div>
<% end %>
history_controller
def create
@history = History.new(history_params)
@history.projects_lkp_id = params[:projects_lkps_id]
respond_to do |format|
if @history.save
format.html{ redirect_to histories_path(id: @history.id), notice:"History added" }
else
@history = History.where(item: @history.item).all
format.html { render 'index' }
end
end
end
def history_params
params.require(:history).permit(:history,:date,:pic)
end
But when I tried to submit form, it says that unpermitted parameter :pictures
Parameter passing is:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xE8d/mD1pXh/2AIKrZlRoL552iEBVZ7jkzLJB1kNZyE=", "history"=>{"history"=>" jhafjkaalkds", "date"=>"20/10/2014", "pictures"=>{"pic"=>[#<ActionDispatch::Http::UploadedFile:0x0000000143d1f8 @tempfile=#<Tempfile:/tmp/RackMultipart20150910-2753-1ml2hmf>, @original_filename="Firefox_wallpaper.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"history[pictures][pic][]\"; filename=\"Firefox_wallpaper.png\"\r\nContent-Type: image/png\r\n">]}}, "projects_lkps_id"=>"", "commit"=>"Submit"}
Unpermitted parameters: pictures
Here the :pic is the attachment field created by using paperclip gem
I have found the answer for that :)
history controller
def new
@histories = History.new
@histories.pictures.build
end
history/new
<%= form_for(@histories, :url => {:action => 'create'}, html: {multipart:true}) do |d| %>
<%= render(:partial =>"form",:locals => {:d => d}) %>
<% end %>
it works :)