gpt4 book ai didi

ruby-on-rails - Rails 4 总是为列添加空值

转载 作者:行者123 更新时间:2023-11-29 14:20:21 24 4
gpt4 key购买 nike

我在 portfolio_files 表中有一个名为 portfolio_id 的列。 portfolio_filesbelongs_to 投资组合表。现在,当我为 portfolio_files 表创建新记录时,它总是使用 null portfolio_id 创建记录。我什至尝试为 portfolio_id 提供静态值,但它仍然无法正常工作。

这是我的 Controller 代码:

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

投资组合文件模型:

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 文件 uploader 提交此表单。

请推荐。提前致谢。

最佳答案

您没有遵循配置约定,您需要使用复数模型名称和 has_many,而不是 portfolio_file 编写 portfolio_files

has_many :portfolio_files, -> { order 'portfolio_files.order' }

希望这对您有所帮助!

关于ruby-on-rails - Rails 4 总是为列添加空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30093203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com