好吧,我要澄清一下
class DuplicatesController < ApplicationController
before_action :set_venue, only: [:new]
def new
end
def create
if @venue.duplicate(venue_params)
flash[:success] = t('controller.create.success',
resource: Venue.model_name.human)
redirect_to admin_venue_url @venue
else
flash[:warning] = @venue.errors.full_messages.to_sentence
render :new
end
end
private
def set_venue
@venue = Venue.friendly.find params[:venue_id]
end
end
def venue_params
params.require(:venue).permit(:name,
:address,
:address_complement,
:city,
:phone)
end
结束
重复重复
(名称, 地址, address_complement, 市, 电话)
new_venue = self.dup
new_venue.update_attributes(description: self.description,
logo: self.logo,
opening_time: self.opening_time,
closing_time: self.closing_time,
ally_id: self.ally_id)
new_venue.save
结束
如何在我的重复控制器中调用这些参数,谢谢 在创建dup之后,我需要设置属性,因为我想保存包含新信息的新记录,但是我不知道该用我的方法来做,有人可以向我解释 谢谢。
答案 0 :(得分:0)
可能最好的方法是仅传递原始模型的id / slug。
然后您的重复项controller.rb看起来类似于:
class DuplicatesController < ApplicationController
def create
old_venue = Venue.friendly.find(params[:id])
venue = old_venue.dup
venue.attributes = venue_params
if venue.save
# success render
else
# error render
end
end
private
def venue_params
params.require(:venue).permit(:permitted_attributes) # id should not be there
end
end
您当然可以重构它,但是我认为在这种情况下不需要它。
或者我最喜欢的是将VenueController#create更改为类似这样,以允许从另一个实例创建:
if copy_from_id = params[:copy_from_id]
@copy_source = Venue.find_by(id: copy_from_id)
@venue = @copy_source.dup
@venue.attributes = venue_params
else
@venue = Venue.new
end
if @resource.save
...
else
...
end