gpt4 book ai didi

ruby-on-rails - 更新失败渲染 :edit. 为什么编辑显示在 :show path?

转载 作者:行者123 更新时间:2023-12-02 04:12:07 24 4
gpt4 key购买 nike

典型的 Rails 更新操作如下所示

def update
respond_to do |format|
if @object.update(object_params)
# success results
else
format.html { render :edit }
end
end
end

因此,我希望如果更新不成功,用户最终将到达路径 /objects/:id/edit

为什么实际路径是/objects/:id,但显示的是编辑表单?

这是正常行为,还是这个应用程序中发生了奇怪的事情?

编辑

#routes.rb

devise_for :users
resources :objects do
resources :children, only: [:index, :new, :create]
member do
get :customaction
end
end

#ability.rb
can :create, Object
can :manage, Object, id: @user.object_id
cannot [:index, :destroy], Object

最佳答案

让我解释一下,

我希望您在编辑表单中注意到它的样子

<form class="edit_post" id="edit_post_1" action="/posts/1" accept-charset="UTF-8" method="post">

表单数据发布在/posts/1;这是浏览器发送表单数据的 URL。

因此,当您的更新任务失败或发生任何验证错误时;您不会重定向 http 请求,而是呈现 edit-form 并返回到浏览器。浏览器会在不更改网址的情况下显示表单。

注意:只有 redirect 会更改网址,render 不会。

如果表单数据提交到 /posts/1/edit 而不是 /posts/1,您的假设是正确的

关于ruby-on-rails - 更新失败渲染 :edit. 为什么编辑显示在 :show path?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36168864/

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