gpt4 book ai didi

ruby-on-rails - 如果 rails 中的 Controller 中有 if 语句,我应该将 response_to 放在哪里?

转载 作者:行者123 更新时间:2023-12-04 03:36:32 25 4
gpt4 key购买 nike

我有一个具有 if 条件的 Controller :

  def update
@contact_email = ContactEmail.find(params[:id])

if @contact_email.update_attributes(params[:contact_email])
flash[:notice] = "Successfully updated contact email."
redirect_to @contact_email
else
render :action => 'edit'
end
end

我在哪里放置 response_to 块:
respond_to do |format| 
format.html {}
format.json {render :json =>@contact_email}
end

最佳答案

def update
@contact_email = ContactEmail.find(params[:id])
ok = @contact_email.update_attributes(params[:contact_email])

respond_to do |format|
format.html {
if ok
flash[:notice] = "Successfully updated contact email."
redirect_to @contact_email
else
render :action => 'edit'
end
}
format.json {
if ok
render :json =>@contact_email
else
...
end
}
end
end

关于ruby-on-rails - 如果 rails 中的 Controller 中有 if 语句,我应该将 response_to 放在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3011709/

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