我可以为 Rails 中的单个请求保存多次数据吗?
有模型调用事件,其中包含以下属性
开始时间, 结束于, 细节, 培训师
这是我生成脚手架命令的正常创建方法
def create
@event = Event.new(params[:event])
respond_to do |format|
if @event.save
format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
format.xml { render :xml => @event, :status => :created, :location => @event }
else
format.html { render :action => "new" }
format.xml { render :xml => @event.errors, :status => :unprocessable_entity }
end
end
end
,当特定事件对象 stat_at 和 end_at 之间的日期差距超过 6 时,我需要按如下方式修改此方法,我需要将该事件保存为两个事件。第一个活动将在原始开始日期开始,结束日期应为活动的中间日期,其他数据相同。但在第二个事件中,开始日期应该是中间日期,结束日期应该是原始结束日期。有人可以解释一下我该怎么做吗???
There is model call Event which contains following attributes
start_at,
end_at,
details,
trainer
This is my normal create method which generated my scaffold command
def create
@event = Event.new(params[:event])
respond_to do |format|
if @event.save
format.html { redirect_to(@event, :notice => 'Event was successfully created.') }
format.xml { render :xml => @event, :status => :created, :location => @event }
else
format.html { render :action => "new" }
format.xml { render :xml => @event.errors, :status => :unprocessable_entity }
end
end
end
and I need to modify this as follows when particular event object have date gap between stat_at and end_at more than 6 I need to save that event as two events. First event will started at original started date and end date should be middle date of the event and other data are same. But in second event it should be start date as middle date and end date should have original end date. Can some one explain how could I done this???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 if @event.save 行之后,您可以像这样创建另一个事件记录
请注意,此代码未经测试。
After the line if @event.save you can create another Event record like this
Please note this code is untested.
当您要执行一些逻辑时,您可以编写一个方法来执行逻辑并从控制器调用该方法。最好将方法保留在事件模型中,以满足胖模型和精益控制器的良好实践。
并从控制器 @event.save 中的控制器 Event.create_event 调用此方法。
这不是经过测试的代码,但我希望您可以轻松地从上面的代码中获得。
As you are going to do some logic, you can write a method to perform your logic and call that method from controller. Better keep the method in Event model to satisfy good practice of Fat Model and Lean Controller.
And call this method from controller Event.create_event in controller @event.save.
This is not tested code but I hope you can easily get from above code.