Rails Active Storage ,How Can I Keep Existing Files / Uploads? - ruby-on-rails

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 %>

Related

Ruby on Rails- gem devise, ArgumentError:wrong number of arguments (given 0, expected 1)

I install devise gem and wanted to add some columns on registration page.
app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
before_action :configure_sign_up_params, only: [:create]
protected
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up){|u| u.permit(:company_id, :name, :email, :profile, :prefecture_id, :address, :password, :password_confirmation)}
end
app/views/devise/registrations/new.html.erb
<%= simple_form_for(resource, as: resource_name, url:registration_path(resource_name)) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.label :campany_id %><br>
<%= f.collection_select :campany_id, Campany.all, :id, :name, include_blank: true %>
<%= f.input :name, required: true, autofocus: true %>
<%= f.inneput :email, paceholder:"メールアドレス", required: true, autofocus: true %>
<%= f.input :profile, required: true, autofocus: true %>
*<%= f.label :prefecture_id %><br>
<%= f.collection_select :prefecture_id, JpPrefecture::Prefecture.all, :code, :name %>
<%= f.input :address, required: true, autofocus: true %>
<%= f.input :password, required: true, hint: ("#{#minimum_password_length} characters minimum" if #minimum_password_length) %>
<%= f.input :password_confirmation, required: true %>
</div>
<div class="form-actions">
<%= f.button :submit, "新規登録" %>
</div>
<% end %>
config/routes.rb
devise_for :users, controllers: {
registrations: 'users/registrations'
}
When I filled out the form and submitted. It show " ArgumentError:wrong number of arguments (given 0, expected 1)" this error!
Is there some wrong?
Thank you!
In new.html.erb, for input email your input spelling should be f.input, also you have given campany_id but in controller you have used company_id.
And in controller:
def configure_sign_up_params
devise_parameter_sanitizer.permit(:sign_up){|u| u.permit(:company_id, :name, :email, :profile, :prefecture_id, :address)}
end
You only need to permit parameters which have been added by you. You do not need to permit password or confirm_password.
Also all the parameters permitted or which are there in form need to be in your table.
Hope this helps.

rails nested attribute do not show in form

I added a nested attribute to my form, the fields for the nested attribute which is Education fields do not render, the other fields do. The relationships appear to be in order and the controller does too.
Here is the code.
Controller
def new
#profile = current_user.build_student_profile
end
def profile_params
params.require(:student_profile).permit(:first_name, :last_name, :gender, student_profiles_attributes: [:degree, :university_id, :major, :major2, :start_date, :end_date, :grade, :grade_scale] )
end
Models
class Education < ActiveRecord::Base
belongs_to :student_profile
belongs_to :university
validates :grade_scale, inclusion: { in: %w(GPA4 GPA7 WAM100) }
validates :degree, :university_id, :major, :start_date, :end_date, :grade, :grade_scale, presence: true
end
class StudentProfile < ActiveRecord::Base
belongs_to :user
has_many :educations
validates :gender, inclusion: { in: %w(male female) }
validates :first_name, :last_name, :gender, presence: true
accepts_nested_attributes_for :educations
end
Form
<%= form_for (#profile) do |f| %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :gender %>
<%= f.text_field :gender %>
<%= f.fields_for :educations do |education_fields| %>
<%= education_fields.label :Degree %>
<%= education_fields.text_field :degree %>
<%= education_fields.label :University %>
<%= education_fields.collection_select(:university_id, University.all, :id, :name) %>
<%= education_fields.label :Major %>
<%= education_fields.text_field :major %>
<%= education_fields.label :Additional_Major %>
<%= education_fields.text_field :major2 %>
<%= education_fields.label :Start_Date %>
<%= education_fields.date_field :start_date %>
<%= education_fields.label :End_Date %>
<%= education_fields.date_field :end_date %>
<%= education_fields.label :Grade %>
<%= education_fields.number_field :grade %>
<%= education_fields.label :Grade_Scale %>
<%= education_fields.select :grade_scale, [["GPA / 4","GPA4"], ["GPA / 7","GPA7"], ["WAM / 100","WAM100"]] %>
<% end %>
<%= f.submit :submit %>
<% end %>
I have tried to add the following to the controller new action #profile.educations.build but I get an error unknown attribute student_profile_id
Can anyone help ?
Make sure you have student_profile_id attribute/column present in educations table.
After that, as you have mentioned, you need to build educations object on student_profile as:
def new
#profile = current_user.build_student_profile
#profile.educations.build
end
Try this
<%= f.fields_for(:educations,#profile.educations.build) do |education_fields| %>
<% end %>
or
def new
#profile = current_user.build_student_profile
#educations = #profile.educations.build
end
<%= f.fields_for(#educations) do |education_fields| %>
<% end %>

Ruby on Rails, require old password to change password

I have implemented a user authentication system in rails using the gem 'bcrypt';I would like to insert a current password field to the edit form to make the changes to the password.
How can do this?
class User < ActiveRecord::Base
before_save { self.email = email.downcase}
before_create :create_remember_token
#Associations
has_one :profile
has_many :posts
#validations
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: {case_sensitive: false}
has_secure_password
validates :password, length: {minimum: 6}
def User.new_remember_token
SecureRandom.urlsafe_base64
end
def User.digest(token)
Digest::SHA1.hexdigest(token.to_s)
end
private
def create_remember_token
self.remember_token = User.digest(User.new_remember_token)
end
end
<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div>
Thank you
In your view file:
<%= f.label :current_password %>
<%= f.password_field :current_password %>
Also make sure you permit the current_password parameter in your controller.
I assumed current_password attr is already defined by has_secured_password.

How to display data from two tables into one form

I use Ruby v 3. I want to display data from 2 tables into one form.
My models:
class Address < ActiveRecord::Base
attr_accessible :city, :number, :street
validates :city, :presence => true
validates :number, :presence => true
validates :street, :presence => true
has_many :users
end
class User < ActiveRecord::Base
belongs_to :address
attr_accessible :name, :phone, :surname, :address_attributes
accepts_nested_attributes_for :address
end
My form looks alike:
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :surname %><br />
<%= f.text_field :surname %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<%= f.fields_for :address_attributes do |p| %>
<div class="field">
<%= p.label :city %><br />
<%= p.text_field :city %>
</div>
<div class="field">
<%= p.label :street %><br />
<%= p.text_field :street %>
</div>
<div class="field">
<%= p.label :number %><br />
<%= p.text_field :number %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
As you can see I use fields_for method. And my controller is here:
def edit
#user = User.find(params[:id])
#user.address_attributes = #user.address
end
It does not working and I totally don't know why. When I click edit on address list i've got an error:
undefined method `with_indifferent_access'
Anyone can help me figure it out?
Check out Episodes 196 and 197 on RailsCasts:
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
There is a revised episode for 196, for which you will need to subscribe to RailsCasts.
I would highly recommend subscribing to learning sites like RailsCasts and CodeSchool, to lear RoR faster and in the right way.
Try to do:
def edit
#user = User.find(parmas[:id])
end
and on your view:
<%= f.fields_for :address do |p| %>
and see if it works, you don't have to add the attributes

rails, carrierwave, multiple images

I am working on my first rails application and I am attempting to setup multiple image uploads in a form. I have created a contest which has many contest entries, and contest entries can have many images. I have the contest entry piece working, but I am having trouble getting the image upload portion of the form working. I am following this tutorial http://lucapette.com/rails/multiple-files-upload-with-carrierwave-and-nested_form/ linked to from the carrier wave wiki.
contest entry model
class ContestEntry < ActiveRecord::Base
attr_accessible :body, :title, :contest_id, :entry_images_attributes
validates :user_id, presence: true
validates :title, presence: true, length: { maximum: 140 }
validates :body, presence: true
validates :contest_id, presence: true
belongs_to :contest
belongs_to :user
has_many :entry_images, as: :imageable
accepts_nested_attributes_for :entry_images
end
Entry image model
class EntryImage < ActiveRecord::Base
attr_accessible :alt, :image_path
belongs_to :imageable, :polymorphic => true
validates :image_path, presence: true
mount_uploader :image, ImageUploader
end
New entry form
<%= nested_form_for([:contest, #entry], :html => {:multipart => true}) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<%= f.hidden_field :contest_id %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<%= f.fields_for :entry_images do |builder| %>
<p>
<%= builder.label :alt %><br />
<%= builder.text_field :alt %>
</p>
<p>
<%= builder.label :image_path %><br />
<%= builder.file_field :image_path %>
</p>
<% end %>
<p><%= f.link_to_add "Add Image", :entry_images %></p>
<%= f.submit "Enter", class: "btn btn-large btn-primary" %>
<% end %>
<%= javascript_include_tag :defaults,"nested_form" %>
for some reason nothing shows up in the <%= f.fields_for :entry_images do |builder| %> block. It is just blank there is also no error message. if I switch it to
<%= f.fields_for :entry_image do |builder| %> and switch to using a form_for instead of the nested_form_for plugin all the fields show up but I get errors when I submit.
Any ideas?
Thanks,
Cory

Resources