Rails 500 xatosi - kuchli parometrlar

Modelni yangilashni qilishga urinayotganda, men kuchli parametr xatolariga ishlayapman. Amaliy ishlarni yaratish, yangilash muvaffaqiyatsiz tugadi.

Started PATCH "/companies/6/users/10" for 71....... at 2017-02-21 06:27:27 +0000
Processing by UsersController#update as JS

Parameters: {"company_id"=>"6", "id"=>"10"}

User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 9], ["LIMIT", 1]]

SQL (3.6ms)  UPDATE "users" SET "last_activity_at" = '2017-02-21 06:27:27.940545' WHERE "users"."id" = $1  [["id", 9]]

User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 10], ["LIMIT", 1]]

Bu xato juda foydali emas, men uchun kam emas :(

Completed 400 Bad Request in 12ms (ActiveRecord: 6.1ms)

ActionController::ParameterMissing (param is missing or the value is empty: user):

app/controllers/users_controller.rb:60:in `user_params'
app/controllers/users_controller.rb:43:in `block in update'
app/controllers/users_controller.rb:42:in `update'

Controller Params

def user_params
  params.require(:user).permit(:name, :role, :email, :company_id, :password, :password_confirmation, :current_password)
end
0
Ushbu yozuvni tuzatish uchun link_to foydalanayapman. Har bir narsani quyidagilar bilan to'ldirish uchun quyidagilarni tuzishni bilmayman: foydalanuvchi parametr. <% = link_to company_user_path (: company_id => current_user.company_id,: id => foydalanuvchi),: method =>: patch, remote: haqiqiy%> <% = qisman qisqartirish: "ruxsat", mahalliy: {role: user .role.to_s.upcase}%> <% end%>
qo'shib qo'ydi muallif martinmsf, manba

6 javoblar

Buning sababi siz formatda params olasiz

{"company_id"=>"6", "id"=>"10"}

Lekin user_params usuli bo'yicha siz quyidagilarga kerak:

{'user' => {"company_id"=>"6", "id"=>"10"}}

shuning uchun params.require (: user) user parametrlarini filtrlaydi. O'ylaymanki, muammo shaklga bog'liq.

3
qo'shib qo'ydi
Bu erda sharhlar asosida, men uni ishlashga muvaffaq bo'ldim. Bu, albatta, ruby-esque ko'rinmaydi. Agar buning uchun yaxshiroq yoki oddiy usul bormi? <% = link_to company_user_path ("user [company_id]" => current_user.company_id, id: user), usul:: yamoq, masofaviy: haqiqiy%%> <% = qisman qisqartirish: "ruxsat", mahalliy: rol: user.role.to_s.upcase}%> <% end%>
qo'shib qo'ydi muallif martinmsf, manba

The problem is in your form . You are not binding the company_id in the user model. Your param should be like {'user' => {'company_id' => 6, 'id' => 10}} and so on... So, bind the values in the user & then resend your ajax.

1
qo'shib qo'ydi

Yoki siz paramlarni quyidagi shaklda olish uchun HTML-ni o'zgartirishingiz kerak.

{"user" => { "company_id"=>"6", "id"=>"10" } }

yordamchidan foydalanib, siz uchun buni amalga oshiradi .

<%= form_for @user do |f| %>

yoki user_params ni o'zgartirishingiz kerak

def user_params
  params.permit(:name, :role, :email, :company_id, :password, :password_confirmation, :current_password)
end

Note: name of your input fields must be something like "user[company_id]" if you want to use params.require(:user).permit(:company_id)

0
qo'shib qo'ydi
Rahmat, albatta, uni qisqartiradi. Parametrlarni link_to tagida yuqorida ko'rsatilgan formatda qanday tuzish mumkin? <% = link_to company_user_path (: company_id => current_user.company_id,: id => user),: method =>: tuzatish, masofaviy: haqiqiy%> <% = qisman ko'rsatish: "ruxsat", mahalliy: rol: user.role.to_s.upcase}%> <% end%>
qo'shib qo'ydi muallif martinmsf, manba

Quyidagi kod sizning ariza parametrlarini (bir marta seriyali holda) bir foydalanuvchi parametriga kiritilishi kerakligini bildiradi.

def user_params
  params.require(:user).permit(:name, :role, :email, :company_id, :password, :password_confirmation, :current_password)
end

Ayniqsa, talab qiladi (: user) , foydalanuvchi kalitini ildiz darajasida bo'lishi kerakligini bildiradi, aks holda bu xato o'chiriladi.

permit(:name, :role, :email, :company_id, :password, :password_confirmation, :current_password)

Argumentlardagi tugmachalarni o'z ichiga olgan xashni qaytaradi.

Shuning uchun {user: {company_id: '6', id: '10'}} ma'lumotlarini taqdim etish bu xatolarni hal qiladi.

Agar siz ushbu natijalarni yaratish uchun ariza ishlatayotgan bo'lsangiz, kirish maydonlari modelName [attributeName] shaklida nomlanishi kerak, shuning uchun user [company_id] .

0
qo'shib qo'ydi

Bu erda sharhlar asosida, men uni ishlashga muvaffaq bo'ldim. Bu, albatta, ruby-esque ko'rinmaydi. Buni qayta yozish uchun yaxshiroq yoki oddiy usul bormi?

<%= link_to company_user_path("user[company_id]" => current_user.company_id, id: user), method: :patch, remote: true do %>
   <%= render partial: "permission", locals: { role: user.role.to_s.upcase } %>
<% end %>
0
qo'shib qo'ydi
<% = link_to company_user_path ({'user' => {company_id: current_user.company_id, id: user}}) usul:: tuzatish moslamasi, masofali: true%>
qo'shib qo'ydi muallif Sravan, manba

Boshqalar tomonidan aytilganidek, yuqori darajadagi kalit user ostida o'ralgan parametrlarga ega bo'lishingiz kerak. Bunga erishishning eng oson usuli - HTMLni o'zgartirmaguningizcha - ActionController :: ParamsWrapper . Buning uchun UsersController ilovasida shunday ishlating:

class UsersController < ApplicationController
  wrap_parameters :user, format: [:json, :xml, :url_encoded_form, :multipart_form]
  …
end

Siz foydalanadigan narsalarga formatlarning ro'yxatini toraysiz. Bundan tashqari, sizda User modelingiz bor deb hisoblasangiz, model nomini ham qoldirishingiz mumkin. Shunday qilib, chiziq wrap_parameters formati: [: url_encoded_form] kabi oddiy bo'lishi mumkin.

0
qo'shib qo'ydi