Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

parameters - Rails "param is missing or the value is empty" error

I have this link_to helper passing an :email_sequence instance and an extra :set_active params.

I then try to update the :email_sequence instance in the controller using strong params but I'm getting an error saying:

param is missing or the value is empty: email_sequence

link_to:

<%= link_to "Activate", admin_email_sequence_path(base_email.email_sequence, set_active: :true), method: :patch %>

Controller:

class Admin::EmailSequencesController < AdminController
  before_action :set_email_sequence

  def update
    if @email_sequence.update(active: email_sequence_params[:set_active])
      flash[:success] = "Sequence updated succesfully"
      redirect_to_forwarder_or(params[:forwarding_uri], admin_account_emails_path)
    end
  end

  private

  def set_email_sequence
    @email_sequence = current_account.email_sequences.find(params[:id])
  end

  def email_sequence_params
    params.require(:email_sequence).permit(:set_active)
  end
end

This is what gets sent in the params:

{"_method"=>"patch", "authenticity_token"=>"[FILTERED]", "set_active"=>"false", "id"=>"1"}

Can anybody tell me what am I doing wrong?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

By params.require(:email_sequence).permit(:set_active) you expect parameters to be { email_sequence: {set_active: "ANY SCALAR VALUE HERE"} } but you pass only set_active you can fix it by permitting the only one parameter

params.permit(:set_active)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...