2

Using strong_params from Rails 4, what is the preferred best way to do this? I used the below solution but are uncertain if this is the best way to do it. ( it works though )

Example:

game_controller.rb ( shortcut version!)

# inside game controller we want to build an Participant object
# using .require fails, using .permits goes true
def GameController < ApplicationController
    def join_game_as_participant
       @participant  = Participant.new(participant_params)
    end
end

def participant_params
    params.permit(:participant,
                  :participant_id,
                  :user_id,
                  :confirmed).merge(:user_id => current_user.id,
                                    :game_id => params[:game_id])
end

1 Answer 1

3

Your participant_params method should be private and you should use the require method :

private
  def participant_params
    params.require(:participant).permit(
      :participant_id, :user_id, :confirmed
    ).merge(
      :user_id => current_user.id, :game_id => params[:game_id]
    )
  end

Hope this help

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

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.