ruby - Rails 5 model .update_or_create form Food cant find id= -


good friday

update edited sources suggest me , include here routes happends when try edit action

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

and log

started "/uzivatel/2017-07-31/edit" 127.0.0.1 @ 2017-07-31 06:46:45 +0200 processing uzivatelcontroller#edit html 

parameters: {"datum"=>"2017-07-31"} user load (0.8ms) select "users".* "users" "users"."id" = $1 order "users"."id" asc limit $2 [["id", 1], ["limit", 1]] order load (0.6ms) select "orders".* "orders" "orders"."datum" = $1 , "orders"."user_id" = $2 order "orders"."id" asc limit $3 [["datum", "2017-07-31"], ["user_id", 1], ["limit", 1]] (0.6ms) begin user load (0.6ms) select "users".* "users" "users"."id" = $1 limit $2 [["id", 1], ["limit", 1]] [i18n-debug] cs.activerecord.models.order => nil [i18n-debug] cs.activerecord.attributes.order.food => nil [i18n-debug] cs.attributes.food => nil [i18n-debug] cs.activerecord.errors.models.order.attributes.food.required => nil [i18n-debug] cs.activerecord.errors.models.order.required => nil [i18n-debug] cs.activerecord.errors.messages.required => nil [i18n-debug] cs.errors.attributes.food.required => nil [i18n-debug] cs.errors.messages.required => "musí existovat" (0.5ms) rollback
rendering uzivatel/edit.html.erb within layouts/application rendered uzivatel/edit.html.erb within layouts/application (3.6ms) completed 500 internal server error in 45ms (activerecord: 4.8ms)

[rollbar] reporting exception: no route matches {:action=>"update", :controller=>"uzivatel", :datum=>nil}, missing required keys: [:datum] [rollbar] scheduling item [rollbar] sending item [rollbar] success [rollbar] details: https://rollbar.com/instance/uuid?uuid=eab60ff5-b63c-417b-aeb7-55fea4242ed6 (only available if report successful) [rollbar] exception uuid saved in env: eab60ff5-b63c-417b-aeb7-55fea4242ed6

actionview::template::error - no route matches {:action=>"update", :controller=>"uzivatel", :datum=>nil}, missing required keys: [:datum]

i use rails 5 , dont know why reports me cant find food id
on line,it looklike dont loads food_id form welcome ideas how rewrite in better way. anyway constructive advices.

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

models:

user model has many orders  food has many orders order belong user ,  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 

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 |date| %>  #calendar helper  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 |form| %> <%= form.collection_select(@food_id, food.all, :id, :name)%> <%= form.submit %> <% end %> 

routes

rails.application.routes.draw '/uzivatel/index', as: 'uzivatel_index' '/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' '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'    # details on dsl available within file, see http://guides.rubyonrails.org/routing.html end 

i found issues. please try fix them , re-run code.

in update action in uzivatelcontroller

use

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

instead of

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

in edit.html.erb

pass object in url_helper

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

if still face error share error trace can debug easily


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -