【问题标题】:Manipulate form data in controller to create new field - Rails在控制器中操作表单数据以创建新字段 - Rails
【发布时间】:2016-06-21 08:29:58
【问题描述】:

这里是菜鸟问题,但我认为它也可以帮助其他人,

我从表单(在视图中)获取数据,在我的控制器中我想使用这个字段:initialgpa 并创建一个新字段:normalisedgpa,然后将其传递回数据库。但是 A)我的数据库永远不会更新和 B)当我添加乘以 2 时,我得到这个错误 undefined method '*' for nil: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”,但没有运气

干杯!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案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
    

    【讨论】:

    • 太棒了,现在更新了标准化的 gpa!然而,在乘法方面,它只是将输入(例如 10.0)转换为 1010.0 而不是 20.0。我对 x * 2 的使用不正确吗?
    • 我更新了我的答案并删除了.to_s,因为这会将数字变成一个字符串,这就是你得到 1010 的原因。
    【解决方案2】:

    作为@Jay-Ar Polidario 上述答案的附加内容,您可能需要将 .to_i 添加到参数中才能获得成功的数学运算。 例如,如果我的 sale_price 的值为“1”,而我只是添加“参数 * 3”,则结果将是“111”而不是 3(1 * 3 = 3)。

    即:

    def sale_params
          modified_params = params.require(:sale).permit(:store_id, :item_id, :quantity, :sale_price)
          def sale_params
           modified_params = params.require(:sale).permit(:store_id, :item_id, :quantity, :sale_price)
     modified_params[:sale_price] =  modified_params[:quantity].to_i * 3.to_i
          modified_params
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      相关资源
      最近更新 更多