Эта кнопка вложена в форму, которая сохраняет названия больниц независимо от основной формы. Я пробовал несколько разных попыток настройки параметров, включая удаление ".require (: больница)". Я попытался добавить «контроллер» и «действие» в Hospital_params. Я пробовал любое другое решение, которое смог найти. Спасибо за помощь!
Ошибка:
Started POST "/hospitals" for ::1 at 2020-07-12 12:42:09 -0500
Processing by HospitalsController#create as */*
{"controller"=>"hospitals", "action"=>"create"}
User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 102], ["LIMIT", 1]]
↳ app/controllers/hospitals_controller.rb:14:in `create'
Completed 400 Bad Request in 7ms (ActiveRecord: 1.6ms | Allocations: 1706)
ActionController::ParameterMissing (param is missing or the value is empty: hospital):
app/controllers/hospitals_controller.rb:48:in `hospital_params'
app/controllers/hospitals_controller.rb:14:in `create'
Параметры:
def hospital_params
params.require(:hospital).permit(:name)
end
Ajax:
$("#save-hospital-button").click(function () {
var hospitalName = $("input[id='transport[hospital_name_input]'").value;
var mydata = {
hospital: {
name: hospitalName
},
};
Rails.ajax({
type: "POST",
url: `/hospitals`,
data: mydata,
success: function (response) {},
error: function (response) {},
});
return false;
})
Контроллер:
def create
puts params
@hospital = current_user.hospitals.new(hospital_params)
respond_to do |f|
if @hospital.save
f.html { redirect_to action: 'index', notice: "Your settings were updated!" }
f.js
else
f.html { render :new, notice: "Your settings were NOT updated!" }
end
end
end
html .erb:
<div class="col-sm-4" id="add_hospital_form">
<%= f.hidden_field current_user.id, id: "current_user" %>
<%= f.text_field "transport[hospital_name_input]", label: "Other facility name:" %>
<button class="btn btn-block btn-success mb-5
" id="save-hospital-button" type="button">Save Facility to List</button>
</div>