0

i am having a problem where i want to upload images Say i have clicked the file_field button and uploaded 'test.jpg' image, now when i click the file_field button to upload another image, it overwrites the previous ones , i want to make it happen so that it keeps on appending images whenever we upload more images instead of replacing itself

by reading docs and other peoples questions ,what i came to know is that i need to add
config.active_storage.replace_on_assign_to_many = false

inside config/application.rb,

few even said we have to put it inside config/environments/development.rb

so i placed that code in both of them and still the file_field keeps overwriting whenever i try to upload it again

pages_controller.eb

class PagesController < ApplicationController

    def index
    end

    # GET to /pages/new
    def new
        @order = Order.new
    end

    # POST to /pages/new
    def create
        # Inserts data into the Orders table
        @order = Order.new(order_params)

    end
    

    private
        def order_params
            params.require(:order).permit(:paper_size, :color, :quantity, :type, :description, :first_name, :last_name, :phone_numnber, :email, contents:[] )
        end
    

end

order.rb (model)

class Order < ApplicationRecord

    validates :paper_size, presence: true
    validates :color, presence: true
    validates :quantity, presence: true
    validates :type, presence: true
    validates :first_name, presence: true
    validates :last_name, presence: true
    validates :phone_number, presence: true
    validates :email, presence: true

    has_many_attached :contents
    
    
end

new.html.erb

<%= form_for @order, url: new_page_path do |f| %>

<div class="container">
    <h1 class="text-center">Order From Home!</h1>
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            
            <%= f.label :first_name%>
            <%= f.text_field :first_name, class:"form-control" %><br/>

            <%= f.label :last_name %>
            <%= f.text_field :last_name, class:"form-control" %><br/>
           

            <%= f.label :phone_number %>
            <%= f.text_field :phone_number, class:"form-control" %><br/>

           
            <%= f.label :email %>
            <%= f.text_field :email, class:"form-control" %><br/>

            <%= f.label :content %>
            <%= f.file_field :content, multiple: true %><br/>
          

            <%= f.label :paper_size %>
             <%= f.select :paper_size, ['A4', 'B4'], { prompt: 'Select' }, class:'form-select' %><br/>

            <%= f.label :color %>
            <%= f.select :color, ['Black & White', 'Color'], { prompt: 'Select' }, class:'form-select' %><br/>

            <%= f.label :quantity %>
            <%= f.select :quantity, options_for_select(0..500), { prompt: "Select" }, class:'form-select' %><br/>

            <%= f.label :description %>
            <%= f.text_area :description, class:"form-control" %><br/>

            <div class="btn-order">
                <%= f.submit %>
            </div>
           
        </div>     
    </div>
</div>

<% end %>
uma
  • 29
  • 7
  • Have you restarted rails server after changing config files ? – steimo Nov 12 '22 at 18:11
  • yes,i have restarted multiple times and still dosent work – uma Nov 13 '22 at 04:58
  • Your create method isn't actually doing anything. `@order = Order.new(order_params)` will just create a new instance of `@order` in memory. If you want to actually persist it to the database you need to call `@order.save`. https://guides.rubyonrails.org/getting_started.html#creating-a-new-article – max Nov 13 '22 at 18:30
  • You also need to change `<%= form_for @order, url: new_page_path do |f| %>` to `<%= form_for @order do |f| %>`. The `new` action/path in Rails is just used to render a form. You create resources by sending a POST request to the collection path (`/pages`). This of course assumes that you haven't added a wonky custom route. https://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default – max Nov 13 '22 at 18:33
  • You're also need to use `f.file_field :contents, multiple: true` not `:content`. – max Nov 13 '22 at 18:39
  • thanks! , i did all of that https://stackoverflow.com/questions/74445596/unpermitted-parameter-phone-number-context-controller-orderscontroller-a – uma Nov 15 '22 at 12:42

0 Answers0