Rails 4总是为列

时间:2015-05-07 06:11:45

标签: ruby-on-rails postgresql ruby-on-rails-4

我在portfolio_id表中有一个名为portfolio_files的列。 portfolio_filesbelongs_to投资组合表。现在,当我为portfolio_files表创建新记录时,它始终使用null portfolio_id创建记录。我甚至尝试为portfolio_id提供静态值,但它仍无效。

这是我的控制器代码:

def new
 @temp = PortfolioFile.new
 @temp.attachment = temp_params['attachment']
 @temp.order = 10
 @temp.portfolio_id = 47
 @temp.save
 respond_to do |format|
  format.js { render :locals => { :temp => @temp, :form_id =>temp_params['form_id']} }
 end
end

private

def temp_params
 params.require(:portfolio_file).permit(:attachment, :form_id, :id,:crop_x,:crop_y,:crop_w,:crop_h,:category_id,:order,:color,:text,:action_type, :portfolio_id)
end

投资组合模型:

class Portfolio < ActiveRecord::Base
 has_many :portfolio_detail
 has_many :portfolio_file, -> { order 'portfolio_files.order' }
 belongs_to :category
 belongs_to :user
end

模式

create_table "portfolio_files", force: :cascade do |t|
 t.string   "attachment_file_name"
 t.string   "attachment_content_type"
 t.integer  "attachment_file_size"
 t.datetime "attachment_updated_at"
 t.integer  "order", limit: 2, default: 0
 t.integer  "portfolio_id", null: false
end

create_table "portfolios", force: :cascade do |t|
 t.integer  "user_id"
 t.integer  "category_id"
 t.integer  "order",       limit: 2
 t.datetime "created_at"
 t.datetime "updated_at"
 t.integer  "form_id",     limit: 2
 t.integer  "viewcount",             default: 0
end

PortfolioFile模型:

class PortfolioFile < ActiveRecord::Base
 belongs_to :portfolio

 has_attached_file :attachment, :styles => lambda { |attachment| {:medium =>  attachment.instance.video? ? { :geometry => "236x236>", :format => 'jpg', :time => 10 } : { :geometry => "236x236>" }, :thumb => attachment.instance.video? ? { :geometry => "150x150>", :format => 'jpg', :time => 10 } : { :geometry => "150x150>" },:large => attachment.instance.video? ? { :geometry => "1000x700", :format => 'jpg', :time => 10 } : { :geometry => "1000x700" }} },:processors => lambda { |a| a.video? ? [ :transcoder ] : [ :cropper ] }

before_post_process :is_audio?

def is_audio?
    return false if [ 'audio/mp3','audio/mpeg'].include?(attachment.content_type) or attachment.instance.action_type.blank?
end

def video?
 [ 'application/x-mp4',
  'video/mpeg',
  'video/quicktime',
  'video/x-flv',
  'flv-application/octet-stream',
  'video/3gpp',
  'video/3gpp2',
  'video/3gpp-tt',
  'video/BMPEG',
  'video/JPEG',
  'video/mp4',
  'video/MP4V-ES',
  'video/MPV',
  'video/mpeg4',
  'video/mpeg4-generic'].include?(attachment.content_type)
 end

 attr_accessor :form_id, :crop_x, :crop_y, :crop_w, :crop_h, :color, :text, :portfolio_id, :action_type

 validates_attachment_content_type :attachment, :content_type => /.*/

 def cropping?
  !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
 end

 def attachment_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file(attachment.path(style))
 end

private
def reprocess_attachment
 attachment.reprocess!
end

形式:

<form method="post" accept-charset="UTF-8" action="/en/portfolio_files/new" enctype="multipart/form-data" class="new_portfolio_file" id="portfolio_work_form3">
 <input type="hidden" value="✓" name="utf8"><input type="hidden" value="fOzK0WL5f00S+QBGD9NAwpHrKGOhm5tUZojDHFXdUfH4ZEIf0QL9sERtI4RdYVRx6EeXS9xYCSL+Lsx3NdZm3A==" name="authenticity_token">
 <input type="file" id="portfolio_file_attachment" multiple="multiple" name="portfolio_file[attachment]">
 <input type="hidden" id="portfolio_file_form_id" name="portfolio_file[form_id]" value="portfolio_work_form3">
 <input type="hidden" id="portfolio_file_portfolio_id" name="portfolio_file[portfolio_id]" value="48">
 <input type="hidden" id="portfolio_file_edit_portfolio" name="portfolio_file[edit_portfolio]" value="48">
 <span class="port_form_error" id="portfolio_work_form3_error"></span
 </form>

我正在使用jQuery文件上传程序提交此表单。

请建议。提前谢谢。

4 个答案:

答案 0 :(得分:2)

您没有遵循约定优于配置,您需要使用带有app.yaml的复数型号名称,而不是has_manyportfolio_file

portfolio_files

希望这有帮助!

答案 1 :(得分:1)

您的投资组合文件中有portfolio_id attr_accessor。你不需要这个。 这将设置一个不会持久化的“虚拟”属性,从而覆盖活动记录的“魔力”。 AR为数据库字段添加了getter和setter,这意味着您无需手动指定attr_accessor

答案 2 :(得分:0)

检查你是否允许在params中使用'portfolio_id'(rails strong属性)

答案 3 :(得分:0)

我认为您的订购可能存在问题......您通常需要断言您想如何订购该系列

has_many :portfolio_files, -> { order 'created_at desc' }

通常您会通过

说明要订购的属性