我试图在“me”/me 部分下命名多个路由,以便在此命名空间下拥有所有基于用户配置文件/帐户的内容,以实现更干净的路由。
解决这个问题的最佳方法是什么?我发现重写默认的 REST 路由(profiles/1/edit)会出现一些问题,比如表单不更新。
路线
get "/me/profile" => "profiles#show", :as => :my_profile
get "/me/profile/:what" => "profiles#edit", :as => :my_profile_edit
post "/me/profile/:what" => "profiles#edit"
ProfilesController
def edit
@profile = current_user.profile
if ["basics", "location", "details", "photos"].member?(params[:what])
render :action => "edit/edit_#{params[:what]}"
else
render :action => "edit/edit_basics"
end
end
def update
@profile = current_user.profile
@profile.form = params[:form]
respond_to do |format|
if @profile.update_attributes!(params[:profile])
format.html { redirect_to :back, :notice => t('notice.saved') }
else
format.html { render action => "/me/profile/" + @profile.form }
end
end
end
如果感觉上面对 REST 原则非常不利。我怎样才能以更好的方式实现想要的结果?
最佳答案
好吧,如果您真的很想坚持使用 REST,我建议您移动 :what
进入 GET params 哈希。然后你可以像这样重写你的路线
scope "/me" do
resource :profile, :only => [:show, :edit, :update]
因此,您将编辑页面命名为
profile_edit_path(:what => "details")
关于您的代码的更多信息。
update_attributes
至 update
render action =>
至 render :template =>
params[:profile]
在您的更新语句中,除非您使用 strong_params。您需要指定从表单中允许哪些参数。这样做是为了防止大规模分配。 认为您真的应该查看 CodeSchool tutorial on Rails 4 .
关于ruby-on-rails - 将/profiles/1/edit/details 重写为/me/profile/details 的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804918/