Question

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
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top