Update use of params

This commit is contained in:
Matt-Yorkley
2023-06-08 19:37:23 +01:00
parent 37a4c73a12
commit 672400192f
4 changed files with 14 additions and 10 deletions

View File

@@ -27,15 +27,15 @@ class VoucherAdjustmentsController < BaseController
end
def add_voucher
if params[:voucher_code].blank?
@order.errors.add(:voucher, I18n.t('split_checkout.errors.voucher_not_found'))
if voucher_params[:voucher_code].blank?
@order.errors.add(:voucher_code, I18n.t('split_checkout.errors.voucher_not_found'))
return false
end
voucher = Voucher.find_by(code: params[:voucher_code], enterprise: @order.distributor)
voucher = Voucher.find_by(code: voucher_params[:voucher_code], enterprise: @order.distributor)
if voucher.nil?
@order.errors.add(:voucher, I18n.t('split_checkout.errors.voucher_not_found'))
@order.errors.add(:voucher_code, I18n.t('split_checkout.errors.voucher_not_found'))
return false
end
@@ -63,4 +63,8 @@ class VoucherAdjustmentsController < BaseController
render status: :unprocessable_entity, cable_ready: cable_car.
replace("#flashes", partial("shared/flashes", locals: { flashes: flash }))
end
def voucher_params
params.require(:order).permit(:voucher_code)
end
end

View File

@@ -14,6 +14,6 @@
%span.formError.standalone
= t("split_checkout.step2.voucher.warning_forfeit_remaining_amount")
- else
= form_with url: voucher_adjustments_path, method: :post, data: { remote: true } do |form|
= form.text_field :voucher_code, value: params[:voucher_code], data: { action: "input->toggle-button-disabled#inputIsChanged" }, placeholder: t("split_checkout.step2.voucher.placeholder"), class: "voucher"
= form_with url: voucher_adjustments_path, model: @order, method: :post, data: { remote: true } do |form|
= form.text_field :voucher_code, value: params.dig(:order, :voucher_code), data: { action: "input->toggle-button-disabled#inputIsChanged" }, placeholder: t("split_checkout.step2.voucher.placeholder"), class: "voucher"
= form.submit t("split_checkout.step2.voucher.apply"), disabled: true, class: "button cancel voucher", "data-disable-with": false, data: { "toggle-button-disabled-target": "button" }

View File

@@ -29,7 +29,7 @@ describe VoucherAdjustmentsController, type: :controller do
describe "#create" do
describe "adding a voucher" do
let(:params) { { voucher_code: voucher.code } }
let(:params) { { order: { voucher_code: voucher.code } } }
it "adds a voucher to the user's current order" do
post :create, params: params
@@ -39,13 +39,13 @@ describe VoucherAdjustmentsController, type: :controller do
end
context "when voucher doesn't exist" do
let(:params) { { voucher_code: "non_voucher" } }
let(:params) { { order: { voucher_code: "non_voucher" } } }
it "returns 422 and an error message" do
post :create, params: params
expect(response.status).to eq 422
expect(flash[:error]).to match "Voucher Not found"
expect(flash[:error]).to match "Voucher code Not found"
end
end

View File

@@ -771,7 +771,7 @@ describe "As a consumer, I want to checkout my order" do
fill_in "Enter voucher code", with: "non_code"
click_button("Apply")
expect(page).to have_content("Voucher Not found")
expect(page).to have_content("Voucher code Not found")
end
end
end