mdiaz00147 mdiaz00147 - 4 months ago 19
Ruby Question

Ruby on rails new object properties

i need to add to my user new objet some new properties that i will define when the form is submited by retrieve them from my database, but when i submited the new properties are nill and i dont know why

def create
plan = params[:user][:plan]
@plan_vars = Plan.find(plan)

@user = User.new(user_params, :plan_id => @plan_vars.id, :lessons => @plan_vars.lessons)
if @user.save
flash[:success] = "Usuario creado exitosamente"
redirect_to users_path
else
flash[:warning] = "Ooops... algo ha fallado :O #{@user.errors}"
redirect_to new_user_path
end
end


here is the user params

def user_params
params.require(:user).permit(:name, :email, :phone, :document, :password,
:password_confirmation)
end

Answer

Here is what to do?

Do this:

@user = User.new(user_params)
@user.plan_id = @plan_vars.id
@user.lessons = @plan_vars.lessons
@user.save

if nested form is used or @plan_vars returns array there results another type of error