Rails 在编辑记录和创建时发送事件?
我的控制器中有以下代码:
# GET /kases/1/edit
def edit
@kase = Kase.find(params[:id])
respond_to do |format|
format.html { render :layout => 'kaseshow'} # show.html.erb
format.xml { render :xml => @kase }
format.pdf { render :layout => false }
end
end
# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
@kase = @company.kases.new(params[:kase])
if @kase.save
UserMailer.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
@kase.create_freeagent_project(current_user) if params[:send_to_freeagent]
redirect_to(@kase)
#flash[:notice] = 'Case was successfully created.'
flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
render :new
end
end
我试图做到这一点,如果用户编辑案例,然后选择“发送到 Freeagent”复选框:
@kase.create_freeagent_project(current_user) if params[:send_to_freeagent]
则案例将重新发送到 Freeagent(在线会计软件)。我不担心处理重复项,因为如果 Freeagent 中已存在该案例,则用户无需重新发送它。
这可能吗?
I have the following code in my controller:
# GET /kases/1/edit
def edit
@kase = Kase.find(params[:id])
respond_to do |format|
format.html { render :layout => 'kaseshow'} # show.html.erb
format.xml { render :xml => @kase }
format.pdf { render :layout => false }
end
end
# POST /kases
# POST /kases.xml
def create
@company = Company.find(params[:kase][:company_id])
@kase = @company.kases.new(params[:kase])
if @kase.save
UserMailer.deliver_makeakase("[email protected]", "Highrise", @kase) if params[:sendtohighrise]
UserMailer.deliver_makeakaseteam("[email protected]", "Highrise", @kase) if params[:notify_team_of_creation]
@kase.create_freeagent_project(current_user) if params[:send_to_freeagent]
redirect_to(@kase)
#flash[:notice] = 'Case was successfully created.'
flash[:notice] = fading_flash_message("Case was successfully created.", 5)
else
render :new
end
end
I am trying to make it so if the user edits a case and then selects the Send to Freeagent tickbox:
@kase.create_freeagent_project(current_user) if params[:send_to_freeagent]
then the case is resent to Freeagent (online accounting software). I'm not worried about dealing with duplicates because if the case already exists in Freeagent then the user won't need to resend it.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的“更新”方法在哪里?仅当您加载数据并呈现用户将看到的用于编辑记录的页面时,才会调用编辑。当在该页面上单击保存/更新/提交时,它应该调用控制器中的“更新”方法。因此,您应该能够在 if 块的真实条件下将同一行放入“update”方法中,如下所示:
Where is your 'update' method? The edit is only called when you are loading the data and rendering the page the user will see to edit the record. When save/update/submit is clicked on that page, it should be calling the 'update' method in the controller. So, you should be able to put that same line into the 'update' method in the true condition of the if block that looks something like this:
是的,您可以在编辑表单中创建复选框/单选/隐藏字段。
如果你想为Kase创建一些模型,你也可以看看accepts_nested_attributes
It is, you could create checkbox/radio/hidden field in your edit form.
If you want to create some models for the Kase, you could as well look at accepts_nested_attributes