Rails 5 model .update_or_create form food cant find id =

时间:2017-07-28 11:04:29

标签: ruby-on-rails ruby forms

耶稣受难日

更新我是您推荐的编辑来源,并在此处包含我的路线,但当我尝试编辑操作时会发生这种情况

No route matches {:action=>"update", :controller=>"uzivatel", :datum=>nil}, missing required keys: [:datum] 

并且日志是

Started GET "/uzivatel/2017-07-31/edit" for 127.0.0.1 at 2017-07-31 06:46:45 +0200
Processing by UzivatelController#edit as HTML
  

参数:{“datum”=>“2017-07-31”}       用户负载(0.8ms)SELECT“users”。* FROM“users”WHERE“users”。“id”= $ 1 ORDER BY“users”。“id”ASC LIMIT $ 2 [[“id”,1],   [“LIMIT”,1]]       订单加载(0.6ms)SELECT“orders”。* FROM“orders”WHERE“orders”。“datum”= $ 1 AND“orders”。“user_id”= $ 2 ORDER BY   “订单”。“id”ASC LIMIT $ 3 [[“datum”,“2017-07-31”],[“user_id”,1],   [“LIMIT”,1]]       (0.6ms)BEGIN用户负载(0.6ms)SELECT“users”。* FROM“users”WHERE“users”。“id”= $ 1 LIMIT $ 2 [[“id”,1],[“LIMIT”,1]]   [i18n-debug] cs.activerecord.models.order =>零[i18n-debug]   cs.activerecord.attributes.order.food =>零[i18n-debug]   cs.attributes.food =>零[i18n-debug]   cs.activerecord.errors.models.order.attributes.food.required =>零   [i18n-debug] cs.activerecord.errors.models.order.required =>零   [i18n-debug] cs.activerecord.errors.messages.required =>零   [i18n-debug] cs.errors.attributes.food.required =>零[i18n-debug]   cs.errors.messages.required => “musírevenovat”(0.5ms)ROLLBACK
  在layouts / application中渲染uzivatel / edit.html.erb已渲染   布局/应用程序中的uzivatel / edit.html.erb(3.6ms)已完成   45毫秒内500服务器内部错误(ActiveRecord:4.8毫秒)

     

[Rollbar]报告异常:没有路由匹配{:action =>“update”,   :controller =>“uzivatel”,:datum => nil},缺少必需的键:[:datum]   [Rollbar]计划项目[Rollbar]发送项目[Rollbar]成功   [Rollbar]详细信息:   https://rollbar.com/instance/uuid?uuid=eab60ff5-b63c-417b-aeb7-55fea4242ed6   (仅在报告成功时可用)[Rollbar]异常uuid   保存在环境中:eab60ff5-b63c-417b-aeb7-55fea4242ed6

     

ActionView :: Template ::错误 - 没有路由匹配{:action =>“update”,   :controller =>“uzivatel”,:datum => nil},缺少必需的键:[:datum]

我使用的是rails 5,我不知道为什么它报告我找不到id为的食物 在线,看起来它没有从形式加载food_id我最终欢迎想法如何以更好的方式重写它。 无论如何,感谢任何建设性的建议。

 @order.update(food_id: Food.find(params[:food_id]))

型号:

User model has many orders 
Food has many orders
Order belong to user and  Food

UzivatelController

class UzivatelController < ApplicationController
  before_action :authenticate_user!
  def index
@date = params[:date] ? Date.parse(params[:date]) : Date.today
@orders = Order.where(:datum => @date.beginning_of_week..@date.end_of_week).where(:user_id => current_user.id)
  end

 def edit
   @order=Order.where(:datum => params[:datum] ).where(:user_id => current_user.id).first_or_create

  end
  def update
  @order=Order.where(:datum => params[:datum] ).where(:user_id => current_user.id).first_or_create
  params.permit(:food_id)
  if @order.update(food_id: Food.find(params[:food_id]).first)
    redirect_to uzivatel_index_path, notice: 'Objednavka uložena'
  else
   redirect_back(fallback_location: uzivatel_index_path)
  end
end


def delete
end
private
def order_params
  params.permit(:food_id)
end

index.html.erb

<div >
<h2 >
<%= link_to "<", date: @date.prev_week %>
<%=l(@date, :format => :monthyear) %>
<%= link_to ">", date: @date.next_week %>
</h2>
<%= calendar @date do |date| %>
 #calendar is helper  to render week calendar 
<% if can_change?(date)%>

<%=link_to l( date),controller: "uzivatel", action: "edit", datum: @date%>
<%else%>
  Nelze vybrat
<%end%>
<%if @orders.ke_dni(date).any?%>
<div class="user_orders_choice">
<%= @orders.ke_dni(date).first.food.name%>
</div>
<%else%>
<div class="user_orders_no_choice">nic</div>
<%end%>
<% end %>

edit.html.erb

<%= form_with url: uzivatel_update_path,method: :put, local: true do |form| %>
<%= form.collection_select(@food_id, Food.all, :id, :name)%>
<%= form.submit %>
<% end %>

路由

Rails.application.routes.draw do
get '/uzivatel/index', as: 'uzivatel_index'
get '/uzivatel/:datum/edit(.:format)', to: 'uzivatel#edit', as: 'uzivatel_edit'
put '/uzivatel/:datum(.:format)', to: 'uzivatel#update', as: 'uzivatel_update'
delete 'uzivatel/:datum(.:format)',to: 'uzivatel#delete', as: 'uzivatel_destroy'
get 'admin/index'
devise_for :users, :path_prefix => 'my'
resources :users , path: 'admin/uzivatele'
resources :suppliers , path: 'admin/dodavatele'
resources :departments , path: 'admin/oddeleni'
resources :svatkies , path: 'admin/svatky'
resources :foods , path: 'admin/nabidky'


 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

1 个答案:

答案 0 :(得分:0)

我发现了一些问题。请尝试修复它们并重新运行代码。

update

中的UzivatelController行动

使用

@order.update(food_id: Food.find(params[:food_id]))

而不是

@order.update(food_id: Food.find(params[:food_id]).first)

edit.html.erb

在url_helper中传递对象

<%= form_with url: uzivatel_update_path(datum: `your dataum value`, food_id: `your food id`), method: :put, local: true do |form| %>
<%= form.collection_select(@food_id, Food.all, :id, :name)%>
<%= form.submit %>
<% end %>

如果仍然遇到任何错误,请共享错误跟踪,以便我们可以轻松调试