I have a partial named _form.html.erb in my alerts/views/ folder. I render that partial from the show page in my agencies/views/ folder.
Everything renders properly, and the partial does create a new alert, but the alert is completely empty.
alerts_controller create method
def create
#alert = Alert.new(body: params[:body],
severity: params[:severity],
agency_id: #current_member.id)
respond_to do |format|
if #alert.save
format.html { redirect_to #alert.agency, notice: 'Alert was successfully created.' }
format.json { render :show, status: :created, location: #alert}
else
format.html { render :new }
format.json { render json: #alert.errors, status: :unprocessable_entity }
end
end
end
_form.html.erb
<%= form_for(Alert.new) do |f| %>
<div class="input-group">
<div class="field">
<%= f.label :body %><br>
<%= f.text_field :body %>
</div>
<div class="field">
<%= f.label :severity %><br>
<%= f.number_field :severity %>
</div>
<div class="action">
<%= f.submit %>
</div>
</div>
<% end %>
There are no errors, except that the alert created is completely empty. The body, severity, and agency_id variables are all nil.
I have tried replacing the line
<%= form_for(Alert.new) do |f| %>
with this:
<%= form_for(#alert) do |f| %>
and adding this line:
#alert = Alert.new
to the show method in the agency controller.
But the same thing happens either way. What am I doing wrong?
EDIT
This is the log starting when I hit submit, and ending before loading the redirect in the alerts.create method.
Started POST "/alerts" for ::1 at 2016-03-31 18:29:43 -0400
Processing by AlertsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"CgpepnYUec9uUoE/Ys6SAkOlzmK+w2q9IN782sXNoXnB3UyuegiS6m+W+mW+nXu4EIL8P8xH6JdigU8FfmzhVw==", "alert"=>{"body"=>"This is the body text", "severity"=>"12345"}, "commit"=>"Create Alert"}
Account Load (0.1ms) SELECT "accounts".* FROM "accounts" WHERE "accounts"."id" = ? LIMIT 1 [["id", 7]]
Agency Load (0.1ms) SELECT "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1 [["id", 2]]
(0.1ms) begin transaction
SQL (0.2ms) INSERT INTO "alerts" ("agency_id", "created_at", "updated_at") VALUES (?, ?, ?) [["agency_id", 2], ["created_at", "2016-03-31 22:29:43.014846"], ["updated_at", "2016-03-31 22:29:43.014846"]]
(135.8ms) commit transaction
Agency Load (0.1ms) SELECT "agencies".* FROM "agencies" WHERE "agencies"."id" = ? LIMIT 1 [["id", 2]]
Redirected to http://localhost:3000/agencies/2
Completed 302 Found in 141ms (ActiveRecord: 136.2ms)
When I comment out the alerts.create method and add this line:
render plain: params
This is the output:
{"utf8"=>"✓",
"authenticity_token"=>"3OfteFX41SV/5NxpTcKbP7AKhLm/ZKah+NXVn84e2xwXMP9wWeQ+AH4gpzORkXKF4y225M3gJIu6imZAdb+bMg==",
"alert"=>{"body"=>"This is the body text.", "severity"=>"12345"},
"commit"=>"Create Alert", "controller"=>"alerts", "action"=>"create"}
The root cause is evident from your params debugging. The params hash has an alert key in it. The alert value is a hash of body and severity.
In your controller, you reference params[:body] and params[:severity]. Those should be params[:alert][:body] and params[:alert][:severity].
Was there a reason not to use Rails' strong parameters? You could refactor to something like:
def create
#alert = Alert.new(alert_params.merge(agency_id: #current_member.id)
…
end
…
private
def alert_params
params.require(:alert).permit(:body, :severity)
end
In the create method params supposed to be like this.
Because your params "alert"=>{"body"=>"This is the body text.", "severity"=>"12345"}
def create
#alert = Alert.new(body: params[:alert][:body],
severity: params[:alert][:severity],
agency_id: #current_member.id)
respond_to do |format|
if #alert.save
format.html { redirect_to #alert.agency, notice: 'Alert was successfully created.' }
format.json { render :show, status: :created, location: #alert}
else
format.html { render :new }
format.json { render json: #alert.errors, status: :unprocessable_entity }
end
end
end
Related
I'm trying to make a frontend mini admin system to reorder the product images stored as an array by Carrierwave multiple upload.
Frontend is working fine.
In my view, I have:
<%= form_for product, url: product_path(product.id), method: :patch do |form| %>
<div class="field">
<%= form.label :images %>
<div class="image-sortable">
<% product.images.each do |image| %>
<div class="image">
<%= hidden_field :product, :images, multiple: true, value: image.cache_name || image.identifier %>
<%= image_tag(image.url, height: 50) %>
<button type="button" class="remove-image">Quitar</button>
</div>
<% end %>
</div>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
Params are being passed correctly
Processing by ProductsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"X0UIytX4V56npfBIrl/fkaUiIgP4BDiJOGpB8tCdAZMeP7kvDXV1z0wsGge4Kch1TJG9Gzhpbtt5hOpQXbMH/Q==", "product"=>{"images"=>["6919-000-6.jpg", "6919-000-2.jpg", "6919-000-3.jpg", "6919-000-4.jpg", "6919-000-5.jpg"]}, "commit"=>"Update Product", "id"=>"1169"}
In the product update, the order of images are not stored, and I'm getting null values appended.
Product Update (0.9ms) UPDATE "products" SET "images" = $1, "updated_at" = $2 WHERE "products"."id" = $3 [["images", "[\"6919-000-2.jpg\",\"6919-000-3.jpg\",\"6919-000-4.jpg\",\"6919-000-5.jpg\",\"6919-000-6.jpg\",null,null,null,null,null]"], ["updated_at", "2023-01-26 21:08:11.373942"], ["id", 1169]]
In my controller, I have the standard update method:
def update
respond_to do |format|
if #product.update(product_params)
format.html { redirect_to #product }
format.json { render :show, status: :ok, location: #product }
else
format.html { render :edit }
format.json { render json: #product.errors, status: :unprocessable_entity }
end
end
end
I have a two forms inside my edit action, one is to update an item inside the item.rb model(which works fine)... and the other one is to create an image in a attachment.rb model. The problem is that it creates a row inside the db images with null values and does not save the actually image along with the correct values.
items_contoller.rb
def edit
#attachment = Attachment.new
end
this is the form for the image create:
<%= form_for #attachment, url: create_attachment_path(#attachment), :html => {:id => "form", :multipart => true }, method: :post do |form| %>
<% if #attachment.errors.any? %>
<div class="centerList">
<div id="error_explanation">
<h2><%= pluralize(item.errors.count, "error") %> <%= t 'store_item_edit_4' %></h2>
<% #attachment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</div>
</div>
<% end %>
<%= hidden_field_tag :item_id, value: #item.id %>
<div class="form-group">
<div class="text-center">
<label class="btn btn-primary"><%= t 'store_item_edit_5' %><span style="display:none;">
<%= form.file_field :image, multiple: true, id: "uploads" %></span></label>
<%= form.submit '', :style => "display: none;" %>
<% end %>
this is the route:
post "attachments/create"=> "attachments#create", :as => :create_attachment
attachments_controller.rb
def create
#attachment = Attachment.new(attachment_params)
respond_to do |format|
if #attachment.save
format.html { redirect_back fallback_location: root_path, notice: 'Image was successfully uploaded.' }
format.json { render :show, status: :created, location: #attachment }
else
format.html { render :new }
format.json { render json: #attachment.errors, status: :unprocessable_entity }
end
end
end
def attachment_params
params.require(:attachment).permit(:item_id, :account_id, :image)
end
and this is what I get inside the console... at some point as you can see I get a Unpermitted parameter: :image:
started POST "/attachments/create" for 127.0.0.1 at 2018-03-14 17:20:23 +0200
Processing by AttachmentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6kgGVoUf2tlM2YdxZis6xavw//zC4azttYi4FFshgw4swiFUIOfb58hCtZxf0If2ihOXz3SCETQSnci6l1IFIA==", "item_id"=>"{:value=>44}", "attachment"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x007fc6e4acfb18 #tempfile=#<Tempfile:/var/folders/hq/pr4rt14n7s31v3f6292wtjm00000gn/T/RackMultipart20180314-4193-1jrij48.jpg>, #original_filename="image1.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"attachment[image][]\"; filename=\"image1.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}
Store Load (0.5ms) SELECT "stores".* FROM "stores" WHERE "stores"."id" = $1 ORDER BY "stores"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
Unpermitted parameter: :image
(0.2ms) BEGIN
SQL (0.4ms) INSERT INTO "attachments" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2018-03-14 15:20:23.368574"], ["updated_at", "2018-03-14 15:20:23.368574"]]
(1.2ms) COMMIT
Redirected to https://localhost:3000/store/items/edit/44
Completed 302 Found in 8ms (ActiveRecord: 2.3ms)
Any ideas how to fix this:
You should add:
def attachment_params
params.require(:attachment).permit(:item_id, :account_id, image: {})
end
i'm making a form to upload multiple images with carrierwave. Save array to db work fine but i want to save 1 array element as 1 record, to make it easier to manage later.
I tried to do as below but it return nilClass err.
All picture use the same description, name is image file name
Anyone has done this kind of stuff before :'(
views/_form
<%= form_for [:admin, #picture], :html => {multipart: true, :class => 'form-horizontal'} do |f| %>
<div class="box-body">
<div class="form-group">
<%= f.label :link, :class => 'col-sm-2 control-label' %>
<div class="col-sm-10">
<%= f.file_field :link, multiple: true, :class => 'form-control', :id => 'imgInp' %>
</div>
</div>
<div class="form-group">
<%= f.label :description, :class => 'control-label col-sm-2' %>
<div class="col-sm-10">
<%= f.text_area :description, :class => 'form-control' %>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<%= link_to admin_pictures_path do %>
<button class="btn btn-default">Back</button>
<% end %>
<%= f.submit nil, :class => 'btn btn-info pull-right' %>
</div>
<!-- /.box-footer --> <% end %>
controller
def create
if params[:link]
params[:link].each { |image|
#picture = Picture.new(name: image.file.filename, description: params[:description], link: image)
#picture.save
}
end
end
respond_to do |format|
if #picture.save
format.html { redirect_to admin_pictures_path, notice: ' picture was successfully created.' }
format.json { render :show, status: :created, location: #picture }
else
format.html { render :new }
format.json { render json: #picture.errors, status: :unprocessable_entity }
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_picture
#picture = Picture.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def picture_params
params.require(:picture).permit(:name, :description, :link)
end
end
param[:link] console
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"2KMy3lPMnf3MqLLHpJAH+Ei40nja+KAJGsx2twyP5L05A95b7rLsZHEFoUSu+CMJQunQ4yUq6kmppdK6I7NQkw==",
"picture"=>{"album_id"=>"3", "team_id"=>"", "link"=>[#<ActionDispatch::Http::UploadedFile:0x007f9095ea1c08 #tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-1h8i98y.jpg>,
#original_filename="images.jpg", #content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"picture[link][]\";
filename=\"images.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f9095ea1be0 #tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-y6d6ug.jpg>, #original_filename="images (1).jpg", #content_type="image/jpeg",
#headers="Content-Disposition: form-data; name=\"picture[link][]\";
filename=\"images (1).jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x007f9095ea1bb8 #tempfile=#<Tempfile:/tmp/RackMultipart20160331-6776-hizrku.jpg>,
#original_filename="lulu.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"picture[link][]\";
filename=\"lulu.jpg\"\r\nContent-Type: image/jpeg\r\n">],
"description"=>"abc"}, "commit"=>"Create Picture"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
params:
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.4ms)
This will solve your issue :
def create
if params[:picture][:link]
params[:picture][:link].each { |image|
#picture = Picture.new(:name => image.original_filename,:description => params[:description])
#picture.store!(image.tempfile)
#picture.save
}
end
end
respond_to do |format|
if #picture.save
format.html { redirect_to admin_pictures_path, notice: ' picture was successfully created.' }
format.json { render :show, status: :created, location: #picture }
else
format.html { render :new }
format.json { render json: #picture.errors, status: :unprocessable_entity }
end
end
end
Try
...
#picture = Picture.new(picture_params)
respond_to do |format|
if #picture.save...
so that #picture.save will not result to nil
I am using following form and controller. If I create a new notification everything gets saved except the campus_id.
It seems to give the wrong campus parameter although I select a different one from the dropdown. If I edit the same entry afterwards then it does get saved? What is going on and how do I fix it?
The same form is used for the edit and create actions. (it is a partial)
It might be worth noting that I use shallow routes for the campus (has_many) and notifications(belongs_to).
routes.rb
shallow do
resources :campus do
resources :notifications
end
end
Form:
<%= form_for [#campus,#notification] do |f| %>
<% if #notification.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#notification.errors.count, "error") %> prohibited this notification from being saved:</h2>
<ul>
<% #notification.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :post %><br>
<%= f.text_area :post %>
</div>
<div class="field">
<%= f.label :campus %><br>
<%= f.collection_select(:campus_id, Campus.all.order('name ASC'), :id, :name, prompt: true) %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
This is the controller:
class NotificationsController < ApplicationController
before_action :set_notification, only: [:show, :edit, :update, :destroy]
before_action :set_campus, only: [:index, :new, :create]
def index
#notifications = #campus.notification
end
def show
end
def new
#notification = #campus.notification.new
end
def edit
end
def create
#notification = #campus.notification.new(notification_params)
respond_to do |format|
if #notification.save
format.html { redirect_to #notification, notice: 'Notification was successfully created.' }
format.json { render action: 'show', status: :created, location: #notification }
else
format.html { render action: 'new' }
format.json { render json: #notification.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #notification.update(notification_params)
format.html { redirect_to #notification, notice: 'Notification was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #notification.errors, status: :unprocessable_entity }
end
end
end
def destroy
#notification.destroy
respond_to do |format|
format.html { redirect_to campu_notifications_url(1) }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_notification
#notification = Notification.find(params[:id])
end
def set_campus
#campus = Campus.find(params[:campu_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def notification_params
params.require(:notification).permit(:post, :campus_id)
end
end
If I look at the log I see the wrong parameter is comitted.
Started POST "/campus/1/notifications" for 84.193.153.106 at
2014-09-29 18:29:33 +0000 Started POST "/campus/1/notifications" for
84.193.153.106 at 2014-09-29 18:29:33 +0000 Processing by NotificationsController#create as HTML Processing by
NotificationsController#create as HTML Parameters: {"utf8"=>"_",
"authenticity_token"=>"oNSlEFeukwEj2hIAT89wFdIYwjHO5c8lzBlCqMyk31Y=",
"notification"=>{"post"=>"sdqfdsfd", "campus_id"=>"3"},
"commit"=>"Create Notification", "campu_id"=>"1"} Parameters:
{"utf8"=>"_",
"authenticity_token"=>"oNSlEFeukwEj2hIAT89wFdIYwjHO5c8lzBlCqMyk31Y=",
"notification"=>{"post"=>"sdqfdsfd", "campus_id"=>"3"},
"commit"=>"Create Notification", "campu_id"=>"1"} Campus Load
(0.4ms) SELECT "campus".* FROM "campus" WHERE "campus"."id" = $1
LIMIT 1 [["id", "1"]] Campus Load (0.4ms) SELECT "campus".* FROM
"campus" WHERE "campus"."id" = $1 LIMIT 1 [["id", "1"]] (0.1ms)
BEGIN (0.1ms) BEGIN SQL (28.6ms) INSERT INTO "notifications"
("campus_id", "created_at", "post", "updated_at") VALUES ($1, $2, $3,
$4) RETURNING "id" [["campus_id", 1], ["created_at", Mon, 29 Sep 2014
18:29:34 UTC +00:00], ["post", "sdqfdsfd"], ["updated_at", Mon, 29 Sep
2014 18:29:34 UTC +00:00]] SQL (28.6ms) INSERT INTO "notifications"
("campus_id", "created_at", "post", "updated_at") VALUES ($1, $2, $3,
$4) RETURNING "id" [["campus_id", 1], ["created_at", Mon, 29 Sep 2014
18:29:34 UTC +00:00], ["post", "sdqfdsfd"], ["updated_at", Mon, 29 Sep
2014 18:29:34 UTC +00:00]] (3.5ms) COMMIT (3.5ms) COMMIT
Might want to change your new and create actions like this:
def new
#notification = #campus.notifications.build
end
def create
#notification = #campus.notifications.build(notification_params)
respond_to do |format|
if #notification.save
format.html { redirect_to #notification, notice: 'Notification was successfully created.' }
format.json { render action: 'show', status: :created, location: #notification }
else
format.html { render action: 'new' }
format.json { render json: #notification.errors, status: :unprocessable_entity }
end
end
end
campus.build_notification will instantiate a notification that belongs_to campus. Using new would require you to pass notification[campus_id] as part of your params.
I'm using this example to create multiple image uploads using Carrierwave Rails 4 multiple image or file upload using carrierwave. For some reason if I edit the Post and try to upload a different image it doesn't update.
listings_controller.rb
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, :except => [:show, :index]
def index
#listings = Listing.order('created_at DESC')
respond_to do |format|
format.html
format.json { render json: #listings }
end
end
def show
#image_attachments = #listing.image_attachments.all
end
def new
#listing = Listing.new
#listing.user = current_user
#image_attachment = #listing.image_attachments.build
end
def edit
end
def create
#listing = Listing.new(listing_params)
#listing.created_at = Time.now
#listing.user = current_user
respond_to do |format|
if #listing.save
params[:image_attachments]['image'].each do |a|
#image_attachment = #listing.image_attachments.create!(:image => a, :listing_id => #listing.id)
end
format.html { redirect_to #listing, notice: 'Post was successfully created.' }
else
format.html { render action: 'new' }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if #listing.update(listing_params)
flash[:notice] = 'Deal was successfully updated.'
format.html { redirect_to #listing }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
def destroy
#listing.destroy
respond_to do |format|
format.html { redirect_to listings_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_listing
#listing = Listing.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def listing_params
params.require(:listing).permit(:condition, :listing_title, :nickname, :listing_size, :listing_price, :user_id, image_attachments_attributes: [:id, :listing_id, :image])
end
end
listing form
<%= form_for(#listing, :html => { :class => 'form', :multipart => true }) do |f| %>
<% if #listing.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#listing.errors.count, "error") %> prohibited this listing from being saved:</h2>
<ul>
<% #listing.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.fields_for :image_attachments do |p| %>
<div>
<%= p.label :image %>
<%= p.file_field :image, :multiple => true, name: "image_attachments[image][]", :class => 'upload' %>
</div>
<% end %>
<div class="actions">
<%= f.submit 'Submit', :class => 'submitButton' %>
</div>
<% end %>
listing.rb
has_many :image_attachments
accepts_nested_attributes_for :image_attachments
Any help? Thanks.
UPDATE
This is the log ouput when I try to update the image field. "about.png" is the new image I'm trying to upload.
Started PATCH "/listings/nike-air-max-90" for 127.0.0.1 at 2014-07-16 11:40:14 -0400
Processing by ListingsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"LU1ADy5JqfuX9CMDtcG/dmGgu9nuvplDQrVixfICsS4=", "listing"=>{"listing_title"=>"Nike Air Max 90", "nickname"=>"", "listing_size"=>"9.5", "listing_price"=>"160", "image_attachments_attributes"=>{"0"=>{"id"=>"1"}}}, "image_attachments"=>{"image"=>[#<ActionDispatch::Http::UploadedFile:0x00000109506810 #tempfile=#<Tempfile:/var/folders/vk/x5f3g8n147z_j39_mzkbfq600000gp/T/RackMultipart20140716-1370-63vlgx>, #original_filename="about.png", #content_type="image/png", #headers="Content-Disposition: form-data; name=\"image_attachments[image][]\"; filename=\"about.png\"\r\nContent-Type: image/png\r\n">]}, "commit"=>"Submit", "id"=>"nike-air-max-90"}
[1m[35mListing Load (0.2ms)[0m SELECT "listings".* FROM "listings" WHERE "listings"."slug" = 'nike-air-max-90' ORDER BY "listings"."id" ASC LIMIT 1
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1[0m
[1m[35m (0.1ms)[0m begin transaction
[1m[36mImageAttachment Load (0.1ms)[0m [1mSELECT "image_attachments".* FROM "image_attachments" WHERE "image_attachments"."listing_id" = ? AND "image_attachments"."id" IN (1)[0m [["listing_id", 2]]
[1m[35m (0.1ms)[0m commit transaction
Redirected to http://localhost:3000/listings/nike-air-max-90
Completed 302 Found in 5ms (ActiveRecord: 0.6ms)
Option 1 (Replace all existing attachments with new uploaded ones
In your update action, you are NOT doing what you are doing in create action. Which is this:
params[:image_attachments]['image'].each do |a|
#image_attachment = #listing.image_attachments.create!(:image => a, :listing_id => #listing.id)
end
You can't expect Rails to do this for you magically because this is not a typical use of accepts_nested_attributes feature. In fact, in your current code, you are not using this feature at all.
If you want to make it work with your current code, you will have to delete all existing image_attachments and create the new ones in the update action, like this:
def update
respond_to do |format|
if #listing.update(listing_params)
if params[:image_attachments] && params[:image_attachments]['image']
# delete existing image_attachments
#listing.image_attachments.delete_all
# create new ones from incoming params
params[:image_attachments]['image'].each do |a|
#image_attachment = #listing.image_attachments.create!(:image => a, :listing_id => #listing.id)
end
end
flash[:notice] = 'Deal was successfully updated.'
format.html { redirect_to #listing }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: #listing.errors, status: :unprocessable_entity }
end
end
end
This will replace all existing images with new ones, if you upload new ones.
Option 2 (Edit them individually)
If you want to be able to update existing attachments, you will have to modify the edit form to allow updating attachment records individually. Or do it via proper use of accepts_nested_attributes feature. Cocoon is one great gem help you incorporate nested attributes in your forms easily.