【问题标题】:Paperclip Issue - can't update and create回形针问题 - 无法更新和创建
【发布时间】:2015-06-13 02:06:25
【问题描述】:

控制器

创建

  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'

型号

  #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/ 

好的,问题来了:

当我在没有文件上传的情况下创建时,它可以工作。但是当我附加文件时,参数不起作用并且它留下空白。所以当我绑定.pry:

[1] pry(#<CourtAgenciesController>)> params
=> {"action"=>"create", "controller"=>"court_agencies"}

参数丢失。


当我在没有文件上传的情况下更新时,它可以工作。但是当我附加文件时,错误是:

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。 还有回形针。

请帮忙。非常需要它。

更新:


路线:

  #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 创建/更新提交?

【问题讨论】:

  • 你能添加路线吗,不知道为什么以前工作的路线现在不工作了。
  • 我更新了我的问题。谢谢

标签: ruby-on-rails file-upload paperclip


【解决方案1】:

你是否在你的模型中定义了attr_accessible...我没有在你的模型中看到这个

attr_accessible: :type,:subtype,:division,:associate_justice,:presiding_justice,:map,:landmark_image,

【讨论】:

  • 基于这里,stackoverflow.com/questions/23437830/…,我们使用强参数而不是 attr_accessible。我的 Rails 版本是 4.1.5
  • 对不起我错过了...@BartSabayton..你在服务器日志中得到了什么..你也可以发布它..你应该得到类似“court_agency”=>{“landmark_image”的东西=>#<:http::uploadedfile:0x007fa7f83cb778>
猜你喜欢
  • 2012-01-17
  • 2011-06-21
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多