开发者

Ruby on Rails form action submitting to /object.1 instead of just /object

开发者 https://www.devze.com 2023-02-04 04:46 出处:网络
https://gist.git开发者_如何学JAVAhub.com/781133 I\'ve got a form that should be submitting to \"/site/:id/detail\", but when I look at the form action it says \"/site/:id/detail.1\" and after hitting

https://gist.git开发者_如何学JAVAhub.com/781133

I've got a form that should be submitting to "/site/:id/detail", but when I look at the form action it says "/site/:id/detail.1" and after hitting the update action (I don't use create from the web so I can't test that) ends up at "/site/:id/detail.1" but I need it at "/site/:id/detail".

I've included the form, the update action and the routes for my application. Any help would be greatly appreciated!


The problem is with how Rails handles path helpers. When you create a singular resource route, it still creates the resource_path helper, but it doesn't expect an object argument because it's a singular resource, not a collection. When you pass in the @path object, its ID is used as the format of the generated path.

Instead of redirect_to resource_path(@resource) use redirect_to resource_path where "single_resource" is the name of the model that this structure is based on.

So in your code, you would want to switch this section to the following…

if @detail.update_attributes(params[:detail])
  format.html { redirect_to(site_detail_path, :notice => 'Details Updated!') }
  format.js { render :layout => false}
else          
  format.html { redirect_to(site_detail_path, :notice => 'Details Not Updated.') }
  format.xml { head :ok }
end
0

精彩评论

暂无评论...
验证码 换一张
取 消