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
更新
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
强有力的参数
def court_agency_params
params.require(:court_agency).permit(
:type,
:subtype,
:division,
:associate_justice,
:presiding_justice,
:map,
:landmark_image,
]
) if params[:court_agency]
end
我的观点
= 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/
好的,这就是问题所在:
当我创建没有文件上传时,它可以工作。但是当我附上文件时,params不工作而且它留空了。所以当我绑定.pry:
[1] pry(#<CourtAgenciesController>)> params
=> {"action"=>"create", "controller"=>"court_agencies"}
Params失踪。
当我在没有文件上传的情况下更新时,它可以工作。但是当我附加文件时,错误是:
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"):
或者就像
Routing Error
No route matches [POST] "/court_agencies/53"
在浏览器上。
我安装了remotipart。 还有Paperclip。
请帮忙。非常需要它。
更新
途径:
#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
此问题与remote: true
有关。如何通过带文件上传的ajax创建/更新提交?
答案 0 :(得分:0)
你是否在你的模型中定义了attr_accessible ...我没有在你的模型中看到
attr_accessible: :type,:subtype,:division,:associate_justice,:presiding_justice,:map,:landmark_image,