Rails 表单适用于创建,而不是更新

发布于 2024-11-13 11:12:57 字数 746 浏览 1 评论 0原文

我有一个城市表单,适用于城市的初始创建,但是当我尝试更新城市时,出现路由错误。

我的routes.rb:

 map.resources :states do |state|
 state.resources :cities
end

形式:

  <% simple_form_for @city, :url => state_cities_path do |f| %>
  <%= f.input :name %>
  <%= f.input :active %>
  <%= f.submit "Save City" %>
  <% end %>

控制器:

  if @city.update_attributes(params[:city])
    format.html { redirect_to state_cities_path(:state_id => @city.state_id) }
    format.xml  { head :ok }

错误:

No route matches "/states/1/cities"

现在,如果我单击地址栏,然后按回车键,它会将我带到它说不存在的路线。创建操作具有相同的redirect_to。

有想法吗?这是 POST 与 PUT 的问题吗?

I have a city form that works for the initial creation of the city, but when I try to update the city, I get a routing error.

My routes.rb:

 map.resources :states do |state|
 state.resources :cities
end

The form:

  <% simple_form_for @city, :url => state_cities_path do |f| %>
  <%= f.input :name %>
  <%= f.input :active %>
  <%= f.submit "Save City" %>
  <% end %>

The controller:

  if @city.update_attributes(params[:city])
    format.html { redirect_to state_cities_path(:state_id => @city.state_id) }
    format.xml  { head :ok }

The error:

No route matches "/states/1/cities"

Now, if I click in the address bar, and hit enter, it takes me right to the route it says doesn't exist. The create action has an identical redirect_to.

Ideas? Is this a problem of POST vs PUT?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2024-11-20 11:12:57

问题是围绕这条线,

 simple_form_for @city, :url => state_cities_path

您没有让表单生成器创建您强制它的正确路线。
表单构建器通过检测是否是新记录来为您传递的实例创建正确的路由,然后在 html 表单的 action 属性中设置正确的路径并设置正确的 http 方法。

似乎您对使用嵌套路由并尝试传递路径/url这一事实感到困惑,因此表单生成器不再检测任何内容并让您设置所有内容,因为表单默认情况下是发布的并且您正在传递 url对于创建操作,它永远不会与更新操作匹配。您仅映射到创建操作,因为更新操作需要这样的 ID

PUT /states/:state_id/cities/:id

,即需要匹配更新路由,但您没有传递 id 或 http 方法。

解决方案?

让表单构建器创建路径,这是通过传递包含您希望表单构建器使用的父实例和子实例的数组来使用嵌套路由来完成的。

simple_form_for [@state,@city] do |f|

不要忘记将一个值放入 @state 中,否则会引发 nil 错误。

现在表单构建器应该正确构建创建和更新操作的正确路径。

顺便说一句,您也可以重定向到这样的数组,

redirect_to [@state,@city]

它将把用户发送到城市的显示操作。

the problem is arround this line

 simple_form_for @city, :url => state_cities_path

you are not letting the form builder create the proper route you are forcing it.
Form builder creates a proper route for the instance you pass by detecting if is a new record,it then sets the proper path in the action attribute of the html form and sets the correct http method.

Seems that you got confused by the fact you are using nested routes and are trying pass the path/url, so the form builder is no longer detecting anything and is letting you set everything, since forms are post by default and you are passing the url to the create action it never matches to the update action. You are only mapping to the create action because the update action requires an ID like this

PUT /states/:state_id/cities/:id

that there is what is needed to match the update route, but you are not passing the id nor the http method.

Solution?

Let the form builder create the path, this is done with nested routes by passing an array with the parent instance and the child instance you want the form builder to use.

simple_form_for [@state,@city] do |f|

dont forget to put a value into @state or it will raise a nil error.

now the form builder should properly build the correct path for the create and update action.

by the way you can also redirect to an array like this

redirect_to [@state,@city]

it will send the user to the show action of the city.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文