1

I am getting the following error when submitting a rails 4 form:

ActionController::ParameterMissing - param is missing or the value is empty: player:

The form is correct. The problem is that it is a one field form, and I want the field to be optional, so that if a user doesn't enter information for the field, clicking submit just goes to the next part of the site. The issue is that I need to sanitize the params using strong parameters. Here is what I have so far:

def presentation_params
    params.require(:player).permit(:name)
end

The problem is that it requires player, and player data is only submitted if the form field is not blank.

I've also tried replacing the word "require" with the word "fetch" but it returns the same error.

How do I allow the user to press submit, still move on, and yet not introduce a security issue by getting rid of strong params?

2 Answers 2

0

You could check whether there are any player parameters in the request prior to using presentation_params. I assume you are doing something like this:

@player.attributes = presentation_params 

In which case you can do:

@player.attributes = presentation_params if params.has_key?(:player)

You may be able to skip more code if there are no player parameters in the request.

Sign up to request clarification or add additional context in comments.

Comments

0

You could try not including name in strong parameters, and create player like this:

Player.create(optional_strong_parameters) do |p|
  p.name = params[:name]||'Default'
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.