我在Rails工作,我有两种模式,一种是预启动,另一种是主动。基本上,我希望用户能够使用预启动的属性创建一个主动。基本上,我想要发生的是,当用户访问他们的启动前,并准备把它变成一个主动,它把他们带到一个表单,已经填充了他们的预启动信息,他们可以添加额外的信息。到目前为止,除了附加的图像(称为:cover_image )之外,我已经为每个属性都做到了这一点。
我认为问题在于,我将主动权的cover_image设置为启动前控制器的新动作的cover_image,但由于这是新的操作而不是创建,所以我还没有保存主动权。我认为这意味着cover_image还没有被重新上传,所以@iniative.cover_image.url没有指向任何东西。它似乎也没有预先填充我的表单的文件字段。
我不完全确定这一切是否可行,但这是客户的要求,所以我试图让它为他们工作。
这是我的控制器:
def new
@initiative = Initiative.new
populate_defaults(@initiative)
@initiative.build_location
3.times{ @initiative.rewards.build }
@initiative.user = current_user
if !params[:prelaunch_id].nil? && !params[:prelaunch_id].empty?
# if user is transferring a prelaunch, assign its attributes to the intiative
@prelaunch = Prelaunch.find(params[:prelaunch_id])
@initiative.assign_attributes(title: @prelaunch.title,
teaser: @prelaunch.teaser,
category: @prelaunch.category,
funding_goal: @prelaunch.funding_goal,
term: @prelaunch.campaign.term,
story: @prelaunch.story,
location: @prelaunch.campaign.location,
video_url: @prelaunch.video_url,
EIN: @prelaunch.campaign.EIN,
nonprofit: @prelaunch.nonprofit,
organization_name: @prelaunch.campaign.organization.name)
end
end编辑:
由于peterept下面的回答,我已经成功地将预启动cover_image放到了表单中,并进入了主动控制器的创建操作。现在的问题是,在create操作中,一切看起来都是完美的:主动权获得了预启动的封面图像,它没有错误地保存,并且重定向到显示动作。
不幸的是,当它到达控制器的显示操作时,@initiative.cover_image再次被设置为默认值。我不知道在成功的创作动作和表演动作之间可能会发生什么。
下面是倡议控制器的创建和显示操作:
def create
if !params[:initiative][:prelaunch_id].nil? && !params[:initiative][:prelaunch_id].empty?
@prelaunch = Prelaunch.find(params[:initiative][:prelaunch_id]) # find the prelaunch if it exists
end
@initiative = Initiative.new(initiatives_params)
@initiative.user = current_user
begin
@payment_processor.create_account(@initiative)
if @initiative.save
# @prelaunch.destroy # destroy the prelaunch now that the user has created an initiative
flash[:alert] = "Your initiative will not be submitted until you review the initiative and then press 'Go Live' on the initiative page"
redirect_to initiative_path(@initiative)
else
flash[:alert] = "Initiative could not be saved: " + @initiative.errors.messages.to_s
render :new
end
rescue Exception => e
logger.error e.message
flash[:error] = "Unable to process request - #{e.message}"
render :new
end
end
def show
@initiative = Initiative.find(params[:id])
@other_initiatives = Initiative.approved.limit(3)
end下面是来自同一个控制器的initiatives_params方法:
def initiatives_params
initiative_params = params.require(:initiative).permit(
:terms_accepted,
:title,
:teaser,
:term,
:category,
:funding_goal,
:funding_type,
:video_url,
:story,
:cover_image,
:nonprofit,
:EIN,
:role,
:send_receipt,
:organization_name,
:crop_x, :crop_y, :crop_h, :crop_w,
location_attributes: [:address],
rewards_attributes: [:id, :name, :description, :donation, :arrival_time, :availability, :_destroy, :estimated_value])
if @prelaunch.media.cover_image
initiative_params[:cover_image] = @prelaunch.media.cover_image
end
initiative_params
end发布于 2015-02-11 02:16:02
您可以传递图像URL并在页面上显示它。
然后,用户可以通过上传一个新图像(按照正常情况)来覆盖这一点。
在您的创建动作,如果他们没有提供一个新的图像,然后设置为在assocoiated的预启动-你会想要复制原来的,这样它就不会被取代,如果他们上传一个新的。(如果您不知道哪个是预启动,则可以将ID向下传递到页面)。
发布于 2020-07-23 17:44:24
我只能通过保存Paperclip对象来使它工作。这是我的模型:
class Grade < ActiveRecord::Base
has_attached_file :certificate
end如果我运行以下命令:
@grade.certificate = new_file
@grade.certificate.save它保存/覆盖文件,但不更新Grade对象。
版本:ruby-2.3.8、Rails 4.2.11.3和paperclip (4.3.6)
https://stackoverflow.com/questions/28445274
复制相似问题