gpt4 book ai didi

ruby-on-rails-3 - 在 rails 中更新表单 - 没有路线匹配 [PUT]

转载 作者:行者123 更新时间:2023-12-04 16:27:42 24 4
gpt4 key购买 nike

我有一个表格来创建广告。

Controller :

  def edit
@engines = Engine.all
@car = Car.find(params[:id])
end

def update
@car = Car.find(params[:id])

if @car.save
redirect_to root_path
end
end

我的路线:
resources :adverts

创建.html.erb
<%= form_for @car, :url => adverts_path do |f| %>

<div><%= f.label :name %><br />
<%= f.text_field :name %></div>

<%= hidden_field_tag :model_id, params[:model_id] %>

<%= select_tag :engine_id, options_from_collection_for_select(@engines, "id", "name",:selected=>@car.engine_id) %>

<div><%= f.submit "Create car!" %></div>
<% end %>

我可以创建广告,但我不能更新它。

编辑.html.erb
<%= form_for @car, :url => adverts_path do |f| %>

<div><%= f.label :name %><br />
<%= f.text_field :name %></div>

<%= hidden_field_tag :model_id, params[:model_id] %>

<%= select_tag :engine_id, options_from_collection_for_select(@engines, "id", "name",:selected=>@car.engine_id) %>

<div><%= f.submit "Update car!" %></div>
<% end %>

当我提交表单时,出现错误 - 没有路由匹配 [PUT] "/adverts"

$ 耙路线:
                       adverts GET    /adverts(.:format)                     adverts#index
POST /adverts(.:format) adverts#create
new_advert GET /adverts/new(.:format) adverts#new
edit_advert GET /adverts/:id/edit(.:format) adverts#edit
advert GET /adverts/:id(.:format) adverts#show
PUT /adverts/:id(.:format) adverts#update
DELETE /adverts/:id(.:format) adverts#destroy

我需要帮助。

最佳答案

当您更新时,您必须通过传递 id 来让 Rails 知道您要更新哪个对象。 .

edit.html.erb改变:

<%= form_for @car, :url => adverts_path do |f| %>

到:
<%= form_for @car, :url => advert_path(@car) do |f| %>

顺便说一句,我发现你的代码很奇怪。为什么您的模型名称与您的 Controller 和路由不匹配?我的意思是您正在制作广告,但您的模型称为汽车。那没有任何意义。要么称其为汽车,要么称其为广告,但不要将它们混为一谈。

关于ruby-on-rails-3 - 在 rails 中更新表单 - 没有路线匹配 [PUT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13639860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com