ruby-on-rails – 在尝试编辑时,Rails form_for导致POST而不是PU
我使用Rails 4并出现以下错误.
我正在传递form_for模型对象使用:meal并且编辑页面正确呈现.但是,Rails似乎没有检查用餐对象是否已经保存,因此它一直尝试将表单发送到#create操作并尝试发出POST请求,而不是将表单发送到更新操作并进行制作点击提交时的PUT请求. 如何让form_for识别我正在尝试更新现有对象并且需要PUT而不是POST?其他一切正常,我已经完成了所有的迁移工作.我对Rails很陌生,而且我几乎整天都在试图自己解决这个问题.请帮忙! 而且要注意的是,当我尝试将模型对象传递为@ meal.log而不是:meal时,Rails再也无法识别:calorie_estimate或:meal_description.将模型对象作为@ meal.log传递给我留下了no方法错误. 餐/ 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,: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 解决方法
问题是您使用的是
nested resources ,因此您对哪些@objects传递给您的form_for感到困惑.
#app/views/meals/edit.html.erb <%= form_for [@log,@meal] do |f| %> 正如你现在所做的那样,传递:用餐是模棱两可的 – 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 #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| %> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |