Help Request
How do I upload image attachment to from an unrelated controller + Strong params requirements?
Background
I have one controller,
Cars
. I have two modelsCar
andGarage
.
Cars
controller creates aGarage
object based on a user's vehicle attributes (users are from devise
). There is noGarages
controller since theCars
controller create a new@garage
object.The
Garage
model is where attributes from theCar
model and other values from theCars
controller are to be stored.The
Cars
show page list all the valuesPlease know this application is working fine as it is except the part where I would like to have a user upload a photo (
paperclip gem
) of their vehicle to thegarages
db.The way this application _supposed_functions is...
- cars/1 This show page contains a list of all vehicle's attributes and a button,
Browse...
and anAdd to Garage
button.- Selecting the
Browse...
button allows you to attach a photo of your vehicle. This is made possible using thepaperclip
gem.- Once your photo is attached, you can select the
Add to Garage
button.- The
Cars
controller creates aGarage
object with several vehicle attributes including some fromdevise
methods, instance variables and places these values into thegarages
table.There are NO errors returned. All of the values are entered into the
garages
db except for the attached image.
Below are the files
cars/cars_controller.rb
class CarsController < ApplicationController
before_action :set_car
def show
@garage = Garage.find(params[:id])
end
def create
@garage = Garage.create( tech_id: @car.service.tech.id, :customer_id: current_customer.id )
if @garage.save
redirect_to techs_path, notice: "You car has been added to the garage!"
elsif
redirect_to tech_path, notice: "Uh oh, flat tire!"
end
end
private
def set_car
@car = Car.find(params[:id])
end
def garage_params
params.permit(:tech_id, :customer_id )
end
end
MODELS
models/car.rb (unsure if the has_attached_file is needed in this model but added anyway)
class Car < ActiveRecord::Base
belongs_to :service
belongs_to :tech #added 5/27
has_attached_file :garage_photo, styles: { medium: "300x300>", :thumb => "100x100>" }#, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :garage_photo, content_type: /\Aimage\/.*\Z/
end
models/garage.rb
class Garage < ActiveRecord::Base
belongs_to :customer
belongs_to :tech
has_attached_file :garage_photo, styles: { medium: "300x300>", :thumb => "100x100>" }#, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :garage_photo, content_type: /\Aimage\/.*\Z/
end
VIEWS
cars/show.html.erb
<h2>Please review to add to garage </h2>
<b>Your garage tech:</b> <%= @car.service.tech.id %><br>
<b>ID:</b> <%= current_customer.id %><br>
<%= form_for @garage, :html => { :multipart => true } do |f| %>
<%= f.file_field :garage_photo %>
<% end %>
<%= button_to 'Add to Garage', tech_cars_path(tech_id: @car.service.tech.id, id: @car.id) %>
<%= link_to 'Back to tech', tech_path(@car.service.tech.id) %>
Routes
Rails.application.routes.draw do
devise_for :customers, controllers: { sessions: 'customers/sessions' }, :path => ''
devise_for :techs, controllers: { sessions: 'techs/sessions' }
#, :path => ''#, :path_names => { :sign_in => "login", :sign_out => "logout" }
resources :techs, only: [:index, :show], shallow: true do
resources :cars, only: [:show, :create]
end
resources :service_menus, :services, :garages
root to: "home#index"
end
6-12 Add more details
_add_attachment_garage_photo_to_garages.rb Migration file
class AddAttachmentGaragePhotoToGarages < ActiveRecord::Migration
def change
change_table :garages do |t|
t.attachment :garage_photo
end
end
end
mysql> describe garages snippet
| garage_photo_updated_at | datetime | YES | | NULL | |
| garage_photo_file_size | int(11) | YES | | NULL | |
| garage_photo_content_type | varchar(255) | YES | | NULL | |
| garage_photo_file_name | varchar(255) | YES | | NULL
| |
Please advise on the issues below:
How can I upload image attachment to
garages
db from an unrelatedCars
controller? Everything gets added to thegarages
db except for theimage attachment
. Please keep in mind there are no errors returned whenAdd to Garage
button is selected. Is this a strong params issue?Are strong params required in the
Cars
controller? If so, how do I assign the db:keys
to instance@variables
? I've searched all over but it's unclear how to assign instance variables to keys for strong params.@garage = Garage.create(garage_params)
params.permit(:tech_id, :customer_id )
If possible, please provide any refactoring approaches you may have.
Any help will be appreciated. Please let me know if you require more details.
Thanks
You need to include
garage_photo
in strong parametercars/cars_controller.rb
cars/show.html.erb