Ruby on Rails,form_for,paperclip和受保护参数的质量分配

时间:2012-06-20 20:22:23

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

我一直在开发一个上传和处理图片的rails应用程序。图像以及其他字符串信息通过form_for提交。我现在已经研究了这个主题大约16个小时,但没有任何解决方案有效。老实说,就像rails甚至没有阅读我的代码。

One Processmodel有许多资产,其中资产只是一个存放一个图像文件的模型。在创建流程模型时,我永远无法访问资产,总是收到无法批量分配attirbutes:assets_attributes

Completed 500 Internal Server Error in 13ms

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: asset):
  app/controllers/process_controller.rb:20:in `new'
  app/controllers/process_controller.rb:20:in `create'

- 此表单用于new.html.erb

<%= semantic_form_for @processmodel, :url => { :action => 'create' }, :html => {     :multipart => true } do |f| %>
    <%= f.input :batch, :as => :string, :name => "Batch" %>
    <%= f.input :batchset, :as => :string, :name => "Batchset" %>
    <%= f.input :numSlots, :as => :number, :name => "Number of slots" %>    
    <%= f.input :key, :as => :file, :name => "Key" %>   

    <%= f.semantic_fields_for :asset do |asset| %> 
        <%= asset.input :asset, :as => :file, :label => "Image" %>
    <% end %><br />

    <%= f.submit %>
<% end %>

-

class Processmodel < ActiveRecord::Base
  attr_accessible :user_id, :batch, 
                :batchset, :numSlots,
                :key,:assets_attributes

  attr_accessor :key_file_name

  has_many :assets, :dependent => :destroy
  belongs_to :user  
  has_attached_file :key
  #    :url => Rails.root.join('/assets/readimages/:basename.:extension'),
  #    :path => Rails.root.join('/assets/readimages/:basename.:extension'),

  accepts_nested_attributes_for :assets, :allow_destroy => true

  . 
  . 
  .

end

-

require 'RMagick'
class Asset < ActiveRecord::Base
    attr_accessible :results_string, 
        :name, 
        :ambiguous_results, 
        :image 
    belongs_to :batch_element
    belongs_to :processmodel
    has_attached_file :image 
    validates_attachment_presence :image
end

-

class ProcessController < ApplicationController
def create
    @Processmodel = Processmodel.new(params[:processmodel])
    @Processmodel.save    
    all_img = Array.new(@processmodel.assets.all)
    respond_to do |format|
      if @processmodel.beginRead(...)
        redirect_to :action => 'results_main', :controller => 'results' 
      else
        format.html { render action: "new" }
   end
end 
end

-

def new
  @processmodel = Processmodel.new
  #5.times{@processmodel.assets.build}
  respond_to do |format|
    format.html #new.html.erb
end
end

我正在请求有关如何解决这个问题并使我的应用运行的想法。

2 个答案:

答案 0 :(得分:0)

您已在此处调用附件:image

has_attached_file :image

但是你在视图中称之为:asset

<%= asset.input :asset, :as => :file, :label => "Image" %>

要修复,只需将此行更改为

即可
<%= asset.input :image, :as => :file, :label => "Image" %>

答案 1 :(得分:0)

您需要更新数据库迁移。运行:

rails g migration AddIdToAsset processmodel_id:integer
rake db::migrate