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
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