CONTROLLER
CREATE
def create
set_cache_buster
@court_agency = CourtAgency.new(court_agency_params)
@court_agency.created_by = current_user
@court_agency.updated_by = current_user
binding.pry
respond_to do |format|
if @court_agency.save
flash[:success] = 'Court was successfully created.'
format.html do
redirect_to court_agencies_path
end
format.js { render :js => "window.location = '#{court_agencies_path}'" }
else
format.json { render json: @court_agency.errors, status: :unprocessable_entity }
end
end
end
UPDATE
def update
set_cache_buster
@court_agency = CourtAgency.find(params[:id])
@court_agency.updated_by = current_user
respond_to do |format|
if @court_agency.update_attributes(court_agency_params)
flash[:success] = 'Court was successfully updated.'
format.html do
redirect_to court_agencies_path
end
format.js { render :js => "window.location = '#{court_agencies_path}'" }
else
format.json { render json: @court_agency.errors, status: :unprocessable_entity }
end
end
end
STRONG PARAMETER
def court_agency_params
params.require(:court_agency).permit(
:type,
:subtype,
:division,
:associate_justice,
:presiding_justice,
:map,
:landmark_image,
]
) if params[:court_agency]
end
MY VIEW
= form_for(@court_agency, remote: true, html: { :multipart => true, class: 'form-horizontal ajax-form', style: 'margin-bottom: 0;', 'data-model-name' => 'court_agency'}) do |f|
.form-group
= f.label :landmark_image, 'Landmark', class: 'control-label'
%br/
= f.file_field :landmark_image, class: 'btn btn-warning'
- if @court_agency.map.present?
= image_tag @court_agency.map.url(:small), class: 'img-responsive img-thumbnail'
.form-group
= f.label :map, 'Map', class: 'control-label'
%br/
= f.file_field :map, class: 'btn btn-warning'
- if @court_agency.map.present?
= image_tag @court_agency.map.url(:small), class: 'img-responsive img-thumbnail'
MODEL
#Paperclip
has_attached_file :map,
:styles => { :large => "900x900>", :medium => "300x300>", :thumb => "196x196>", :small => '50x50>' },
:default_url => ActionController::Base.helpers.asset_path('missing.png'),
:url => "/assets/court_agencies/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/court_agencies/:id/:style/:basename.:extension"
validates_attachment_content_type :map, :content_type => /\Aimage\/.*\Z/
has_attached_file :landmark_image,
:styles => { :large => "900x900>", :medium => "300x300>", :thumb => "196x196>", :small => '50x50>' },
:default_url => ActionController::Base.helpers.asset_path('missing.png'),
:url => "/assets/court_agencies/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/court_agencies/:id/:style/:basename.:extension"
validates_attachment_content_type :landmark_image, :content_type => /\Aimage\/.*\Z/
Ok here's the problem:
When I create without file upload, it works. But when I attach file, params not working and it leaves blank. So when I binding.pry:
[1] pry(#<CourtAgenciesController>)> params
=> {"action"=>"create", "controller"=>"court_agencies"}
Params is missing.
When I update without file upload, it works. But when I attach file, the error is:
Started POST "/court_agencies/53" for 127.0.0.1 at 2015-04-08 16:03:21 +0800
ActionController::RoutingError (No route matches [POST] "/court_agencies/53"):
or just like
Routing Error
No route matches [POST] "/court_agencies/53"
on browser.
I have remotipart installed. Also Paperclip.
Please help. Need it badly.
UPDATE:
ROUTES:
#COURT AGENCY
resources :court_agencies do
member do
get 'list'
put 'update_uin'
end
collection do
get 'get_court_agency_list'
get 'court_agency_list'
get 'add'
get 'get_uin'
end
end
This issue is about remote: true
. How is it possible to create/update submitting via ajax with file upload?