Noob在这里提问,但我认为它也可以帮助其他人,
我从表单(在视图中)和我的控制器中获取数据我想使用此字段:initialgpa并创建一个新字段:normalisedgpa然后将其传递回数据库。但是A)我的数据库永远不会更新和B)当我将乘法加2时,我得到这个错误未定义的方法' *'为零:NilClass
以下是我的控制器代码
def update
@studentprofile = StudentProfile.find_by(id: params[:id])
@studentprofile.update_attributes(student_profile_params)
redirect_to @studentprofile
end
def student_profile_params
params[:normalisedgpa] = params[:initialgpa].to_s * 2
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
我也试过放置" params [:normalisedgpa] = params [:initialgpa] * 2"在更新方法中,以及删除" .to_s"没有运气
干杯!
答案 0 :(得分:1)
控制器/ 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
答案 1 :(得分:0)
作为@ Jay-Ar Polidario的上述答案的补充,您可能需要在参数中添加.to_i才能获得成功的数学运算。 例如,如果我的sale_price的值为“1”而我只是添加“参数* 3”,则结果将为“111”而不是3(1 * 3 = 3)。
即:
$userId