在尝试编辑时,Rails form_for会导致POST而不是PUT

时间:2016-02-03 23:19:05

标签: ruby-on-rails ruby ruby-on-rails-4

我正在使用 Rails 4 并出现以下错误。

  

路由错误
  没有路线匹配[POST]“/ logs / 1 / meal / 13 / edit

我正在传递form_for模型对象使用:meal并且编辑页面正确呈现。但是,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

的routes.rb

Rails.application.routes.draw do

  root to: 'logs#index'

  resources :logs do
    resources :meals
  end
end

schema.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

3 个答案:

答案 0 :(得分:4)

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

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

如你所知,传递:meal含糊不清 - Rails无法识别发送其提交的路由/方法,因为它没有可用的数据。

如果您想更新一个对象,您必须将相应的数据传递给表单,包括对象的{{1 }}:

id

如Rails为object orientated,您最好将实际对象传递给<%= form_for :meal, url: { controller: "meals", action: "update", id: "5" }, method: :put do |f| %>

form_for

-

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

<%= form_for @meal ...

这意味着resources :logs do resources :meals #-> url.com/logs/:log_id/meals/:id end 的{​​{3}}值:

form

答案 1 :(得分:0)

你得到的错误:

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

表示您的表单执行POST而不是PUT。

要使这项工作正常,只需将method: :put添加到form_for声明:

<%= form_for(:meal, method: :put) do |f| %>

答案 2 :(得分:0)

如果您的控制器正在创建名为@meal的实例变量,则应使用该变量而不是符号。所以写:

form_for @meal do |f| 

然后rails可以查询实例变量以查看它是new_record?(在这种情况下它将POST数据)还是现有记录(最有可能是PATCH)。

要构建嵌套路由,您需要设置一个实例变量@log(我在您的代码中没有看到,但您可能已经这样做了),然后您可以写:

form_for [@log, @meal] do |f| 

将计算正确的路径。