如何解决错误发生后进行渲染编辑时,Rails将id附加到单个路径上
|| 我有以下单一路线:scope \'/seller\' do
resource :seller_profile,:path => \"/profile\",:only => [:show,:edit,:update]
end
和以下控制器:
class SellerProfilesController < ApplicationController
before_filter :validate_user_as_seller
def show
@seller_profile = current_user.seller_profile
end
def edit
@seller_profile = current_user.seller_profile
end
def update
@seller_profile = current_user.seller_profile
if @seller_profile.update_attributes(params[:seller_profile])
redirect_to(seller_profile_path,:notice => \'Profile was successfully updated.\')
else
render :action => \"edit\"
end
end
end
鉴于必须先对用户进行身份验证才能获得对控制器的访问权限,所以我使用单数路由,因此我可以从登录用户处获取Seller_profile。
这就像一种魅力,只有一个问题。当我编辑vendor_profile并发生验证错误时,将再次编辑表单,并正确显示错误。问题在于,rails会将已编辑记录的ID附加到url。例如,
当我第一次编辑记录时,URL为:
http://0.0.0.0:3000/seller/profile/edit
但是如果表单提交时出现验证错误,则表单本身会重新显示在
http://0.0.0.0:3000/seller/profile.2
其中2是要编辑的记录的ID。
形式如下:
<%= simple_form_for @seller_profile do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>
如前所述,一切都很好,但是我会完全屏蔽URL中的ID。我该怎么办?
解决方法
我对simple_form_for的工作还不是很充分。但似乎总是在猜测您的网址,好像它们不是单一资源一样。您可以提供一个自定义的:
<%= simple_form_for @seller_profile,:url => seller_profile_path do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。