gpt4 book ai didi

ruby-on-rails - 尝试编辑时,Rails form_for 结果为 POST 而不是 PUT

转载 作者:数据小太阳 更新时间:2023-10-29 07:07:01 27 4
gpt4 key购买 nike

我正在使用 Rails 4 并遇到以下错误。

Routing Error
No route matches [POST] "/logs/1/meals/13/edit

我正在使用 :meal 传递模型对象的 form_for,并且编辑页面正确呈现。但是,Rails 似乎并没有检查膳食对象是否已经保存,因此它一直尝试将表单发送到 #create 操作并尝试发出 POST 请求,而不是将表单发送到更新操作并进行当我点击提交时一个 PUT 请求。

我如何让 form_for 识别我正在尝试更新现有对象并且需要 PUT 而不是 POST?其他一切正常,我已经运行了所有迁移。我是 Rails 的新手,几乎一整天都在尝试自己解决这个问题。请帮忙!

请注意,当我尝试将模型对象作为 @meal.log 而不是 :meal 传递时,Rails 不再能够识别 :calorie_estimate 或 :meal_description。将模型对象作为 @meal.log 传递给我留下了无方法错误。

膳食/edit.html.erb

<h3> EDIT MEAL </h3>
<%= form_for(:meal) do |f| %>
<div id="meal-form">
<%= f.text_field :calorie_estimate, class: 'meal-form-fields', :placeholder => "Calorie Estimate" %>
<%= f.text_field :meal_description, class: 'meal-form-fields', :placeholder => "Food Description" %>
<div class="submit-form" style="width: 75px; height: 15px;">
<%= f.submit 'UPDATE', :class => 'submit-form-text' %>
</div>
</div>
<% end %>

meals_controller.rb

class MealsController < ApplicationController
include MealsHelper

def create
@meal = Meal.new(meal_params)
@meal.log_id = params[:log_id]
@meal.save

redirect_to log_path(@meal.log)
end

def edit
@meal = Meal.find(params[:id])
end

def update
@meal = Meal.find(params[:id])
@meal.update(meal_params)
redirect_to log_path(@log)
end

def meal_params
params.require(:meal).permit(:calorie_estimate, :meal_description)
end

end

可能的路线:

Prefix Verb   URI Pattern                            Controller#Action
root GET / logs#index
log_meals GET /logs/:log_id/meals(.:format) meals#index
POST /logs/:log_id/meals(.:format) meals#create
new_log_meal GET /logs/:log_id/meals/new(.:format) meals#new
edit_log_meal GET /logs/:log_id/meals/:id/edit(.:format) meals#edit
log_meal GET /logs/:log_id/meals/:id(.:format) meals#show
PATCH /logs/:log_id/meals/:id(.:format) meals#update
PUT /logs/:log_id/meals/:id(.:format) meals#update
DELETE /logs/:log_id/meals/:id(.:format) meals#destroy
logs GET /logs(.:format) logs#index
POST /logs(.:format) logs#create
new_log GET /logs/new(.:format) logs#new
edit_log GET /logs/:id/edit(.:format) logs#edit
log GET /logs/:id(.:format) logs#show
PATCH /logs/:id(.:format) logs#update
PUT /logs/:id(.:format) logs#update
DELETE /logs/:id(.:format) logs#destroy

路线.rb

Rails.application.routes.draw do

root to: 'logs#index'

resources :logs do
resources :meals
end
end

架构.rb

ActiveRecord::Schema.define(version: 20160128205351) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "logs", force: :cascade do |t|
t.string "entry_date"
t.integer "calorie_goal"
t.string "notes"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "meals", force: :cascade do |t|
t.integer "calorie_estimate"
t.string "meal_description"
t.integer "log_id"
t.datetime "created_at"
t.datetime "updated_at"
end

end

最佳答案

问题是您使用的是 nested resources ,因此您对将哪些 @objects 传递给您的 form_for 感到困惑。

#app/views/meals/edit.html.erb
<%= form_for [@log, @meal] do |f| %>

正如您目前所拥有的,传递 :meal 是不明确的 - Rails 无法辨别将其提交发送到的路由/方法,因为它没有该数据可用。

如果您想更新一个对象,您必须将适当的数据传递给表单,包括对象的id:

<%= form_for :meal, url: { controller: "meals", action: "update", id: "5" }, method: :put do |f| %>

比如Rails就是object orientated ,您最好将实际对象传递给您的 form_for:

<%= form_for @meal ...

--

您遇到的问题是您有一个嵌套资源:

resources :logs do
resources :meals #-> url.com/logs/:log_id/meals/:id
end

这意味着 you need to pass both the Log and Meal您的 form 的值:

#app/controllers/meals_controller.rb
class MealsController < ApplicationController
def edit
@log = Log.find params[:log_id]
@meal = Meal.find params[:id]
end

def update
@log = Log.find params[:log_id]
@meal = Meal.find params[:id]
@meal.update meal_params
end
end

#app/views/meals/edit.html.erb
<%= form_for [@log, @meal] do |f| %>

关于ruby-on-rails - 尝试编辑时,Rails form_for 结果为 POST 而不是 PUT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35189653/

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