tde*_*lam 3 ruby-on-rails paperclip ruby-on-rails-3
我正在尝试将超过1个文件资源上传到电影中.我一直收到错误Asset model missing required attr_accessor for 'asset_file_name',我不知道为什么,这是我的代码:
asset.rb模型
class Asset < ActiveRecord::Base
belongs_to :movie
has_attached_file :asset
end
Run Code Online (Sandbox Code Playgroud)
movie.rb模型
class Movie < ActiveRecord::Base
belongs_to :user
has_many :assets
accepts_nested_attributes_for :assets
validates :title, presence: true
default_scope order: 'movies.created_at DESC'
end
Run Code Online (Sandbox Code Playgroud)
我还运行rails g paperclip movie asset了生成以下迁移文件
class AddAttachmentAssetToMovie < ActiveRecord::Migration
def self.up
add_column :movies, :asset_file_name, :string
add_column :movies, :asset_content_type, :string
add_column :movies, :asset_file_size, :integer
add_column :movies, :asset_updated_at, :datetime
end
def self.down
remove_column :movies, :asset_file_name
remove_column :movies, :asset_content_type
remove_column :movies, :asset_file_size
remove_column :movies, :asset_updated_at
end
end
Run Code Online (Sandbox Code Playgroud)
这是我生成资产模型时的"创建资产"迁移:
class CreateAssets < ActiveRecord::Migration
def change
create_table :assets do |t|
t.string :asset_file_name
t.integer :asset_file_size
t.string :asset_content_type
t.datetime :asset_updated_at
t.text :asset_description
t.integer :movie_id
t.timestamps
end
end
Run Code Online (Sandbox Code Playgroud)
可以提出为什么我会收到错误Asset model missing required attr_accessor for 'asset_file_name'吗?
较新版本的Rails(正确)偏执于质量分配.添加attr_accessible :asset_file_nameAsset模型以声明数据库可以接受请求的外部参数输入.
为了更加安全,取消config.active_record.whitelist_attributes = true你的application.rb(然后测试一切),让你不得不去思考"是啊,那很好,没有什么大不了的事情"了,你让用户更新任何属性.
| 归档时间: |
|
| 查看次数: |
5954 次 |
| 最近记录: |