我有以下单一路线:
scope '/seller' do
resource :seller_profile, :path => "/profile", :only => [:show, :edit, :update]
end
和以下 Controller :
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
鉴于用户在访问 Controller 之前必须经过身份验证,因此我使用单一路由,因此我可以从登录用户那里获取 seller_profile。
这很有效,只有一个问题。当我编辑 seller_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 %>
关于ruby-on-rails-3 - 错误后渲染编辑时,Rails 将 id 附加到单一路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111038/