控制器/student_profiles_controller.rb
def update
@studentprofile = StudentProfile.find_by(id: params[:id])
@studentprofile.update(student_profile_params)
redirect_to @student_profile
end
def student_profile_params
modified_params = params.require(:student_profile).permit(:status,:name,:imagethumbnail,:aboutme, :country, :state, :city,:language, :age,:gender,:initialgpa,:normalisedgpa,:universityname,:degree ,:degreetype ,:countryofdegree,:workexperience ,:wantstoworkin,:hasworkexperiencein,:permissiontoworkin,:currentlyemployed,:referencesuponrequest ,:worktype,:monthsspentabroadworking,:monthsspentabroadliving,:charitywork)
modified_params[:normalisedgpa] = modified_params[:initialgpa] * 2
modified_params
end
建议
控制器/student_profiles_controller.rb
def update
# use camelcase->underscore variable naming (i.e. @student_profile) instead of @studentprofile
# use .find instead of .find_by so that it will show a Not Found page instead when such StudentProfile does not exist anymore
@student_profile = StudentProfile.find(params[:id])
# modifies @student_profile with the param values, but does not save yet to the database
@student_profile.assign_attributes(student_profile_params)
# manipulate the values here in the action and do not manipulate the params
@student_profile.normalisedgpa = @student_profile.initialgpa * 2
# handle validation errors, rather than silently failing when there is a validation error
if @student_profile.save # if saving to the database successful
# use flash messages (this is optional depending on your code)
redirect_to @student_profile, success: 'Student Profile updated'
else # if saving to the database not successful
# the line below is just an example depending on your code
render :edit
end
end
private
def student_profile_params
params.require(:student_profile).permit(:status,:name,:imagethumbnail,:aboutme, :country, :state, :city,:language, :age,:gender,:initialgpa,:normalisedgpa,:universityname,:degree ,:degreetype ,:countryofdegree,:workexperience ,:wantstoworkin,:hasworkexperiencein,:permissiontoworkin,:currentlyemployed,:referencesuponrequest ,:worktype,:monthsspentabroadworking,:monthsspentabroadliving,:charitywork)
end