从Rails中的原始HTML组件获取Params

时间:2015-11-27 05:32:12

标签: ruby-on-rails ruby

我正在制作一个输入表单,其功能不太适合常规的form_for模板rails提供。我想我会编写自己的HTML来模仿form_for的html输出,但是嵌入了其他form_for元素。

对于我可以使用rails框架的字段。对于其他人,我创建了隐藏字段来存储Rails的内容,以及常规输入字段,其值使用JavaScript操作以放入隐藏字段。

这是一个这样的领域:

State:<br>
<input type="text" class = "state name_input administrative_area_level_1">
<div class="field">
  <input type="hidden" name="address[state]" id="state">
</div>

当我将隐藏字段的值发送到控制台时,我得到了很好的回复:

state 37

这意味着州字段保存值37.完美。

除了当我试图制作我的模型时,参数是空的:

"state"=>"",

我确实有一个无法隐藏的字段:

Street:<br><input type="text" id="street" name="address[street]">

所以我将状态输入类型更改为数字,如果它没有被隐藏,那就是它的结果。为什么不能找到我的参数?

2 个答案:

答案 0 :(得分:0)

您可以显示更多详细信息。我不能理解你做的100%事情,因为你写的很简单。但我想你需要测试参数名称是地址[状态],但它不仅仅是状态,因为如果只有状态当然是零。
如果你的html渲染相同,那么我建议你应该在控制器中放一个调试,以便在提交表单时看到params返回控制器。 例如:在控制器中添加行:

def main():
       inputList = get_user_input()
       average = get_scores(inputList)
       weightedAvgs = get_weightedavg(average, inputList)
       results = get_class_grade(weightedAvgs)

       ans = "yes"
       while(ans == "yes"):
            def get_user_input():
                    inputList = [] # initializes a list
            # Gets how many scores for each category the user would like to enter and adds value to a list
                    tests = int(input("How many tests scores would you like to enter? "))
                    inputList.append(tests)
                    assignments = int(input("How many assignment scores would you like to enter: "))
                    inputList.append(assignments)
                    quizzes = int(input("How many quiz scores would you like to enter? "))
                    inputList.append(quizzes)
                    labs = int(input("How many lab scores would you like to enter? "))
                    inputList.append(labs)
                    final = int(input("How many final scores would you like to enter? "))
                    inputList.append(final)

            # Gets the weight of each category if there are any scores to enter and adds the value to a list
                    if tests > 0:
                            testWeight = float(input("Enter the weight of tests: "))
                            inputList.append(testWeight)
                    else:
                            testWeight = 0
                    if assignments > 0:
                            assignmentWeight = float(input("Enter the weight of assignments: "))
                            inputList.append(assignmentWeight)
                    else:
                            assignmentWeight = 0
                    if quizzes > 0:
                            quizWeight = float(input("Enter the weight of quizzes: "))
                            inputList.append(assignmentWeight)
                    else:
                            quizWeight = 0
                    if labs > 0:
                            labWeight = float(input("Enter the weight of labs: "))
                            inputList.append(labWeight)
                    else:
                            labWeight = 0
                    if final > 0:
                            finalWeight = float(input("Enter the weight of the final: "))
                            inputList.append(finalWeight)
                    else:
                            finalWeight = 0

                    return(inputList)

            def get_scores(inputList):
            # Gets scores for each category & calculates avg for each category
                    average = []
                    testScoreList = []
                    tests2 = inputList[0]
                    for x in range(tests2):
                            testScore = float(input("Enter your test score: "))
                            testScoreList.append(testScore)
                    if tests2 == 0:
                            testAvg = 0
                    else:
                            testAvg = sum(testScoreList) / inputList[0]
                    average.append(testAvg)


                    assignmentScoreList = []
                    assignments2 = inputList[1]
                    for x in range(assignments2):
                            assignmentScore = float(input("Enter your assignment score: "))
                            assignmentScoreList.append(assignmentScore)
                    if assignments2 == 0:
                            assignmentAvg = 0
                    else:
                            assignmentAvg = sum(assignmentScoreList) / inputList[1]
                    average.append(assignmentAvg)


                    quizScoreList = []
                    quizzes2 = inputList[2]
                    for x in range(quizzes2):
                            quizScore = float(input("Enter your quiz score: "))
                            quizScoreList.append(quizScore)
                    if quizzes2 == 0:
                            quizAvg = 0
                    else:
                            quizAvg = sum(quizScoreList) / inputList [2]
                    average.append(quizAvg)


                    labScoreList = []
                    labs2 = inputList[3]
                    for x in range(labs2):
                            labScore = float(input("Enter your lab score: "))
                            labScoreList.append(labScore)
                    if labs2 == 0:
                            labSum = 0
                    else:
                            labSum = sum(labScoreList)
                    average.append(labSum)


                    finalScoreList = []
                    final2 = inputList[4]
                    for x in range(final2):
                            finalScore = float(input("Enter the score for your final: "))
                            finalScoreList.append(finalScore)
                    if final2 == 0:
                            finalAvg = 0
                    else:
                            finalAvg = sum(finalScoreList) / inputList[4]
                    average.append(finalAvg)


            def get_weighted_avg(average, inputList):
                    weightedAvgs = []

                    weightedTestAvg = average[0] * inputList[5]
                    weightedAvgs.append(weightedTestAvg)
                    print("Your weighted average is " + str(weightedTestAvg))

                    weightedAssignmentAvg = average[1] * inputList[6]
                    weightedAvgs.append(weightedAssignmentAvg)
                    print("Your weighted average is " + str(weightedAssignmentAvg))

                    weightedQuizAvg = average[2] * inputList[7]
                    weightedAvgs.append(weightedQuizAvg)
                    print("Your weighted average is " + str(weightedAssignmentAvg))

                    weightedLabSum = average[3] * inputList[8]
                    weightedAvgs.append(weightedLabSum)
                    print("The sum of your lab scores are " + str(weightedLabAvg))

                    weightedFinalAvg = average[4] * inputList[9]
                    weightedAvgs.append(weightedFinalAvg)
                    print("Your weighted average is " + str(weightedFinalAvg))

                    return(weightedAvgs)

            def get_class_grade(weightedAvgs):
                    grade = weightedAvgs[0] + weightedAvgs[1] + weightedAvgs[2] + weightedAvgs[3] + weightedAvgs[4]

                    if grade >= 90:
            finalGrade = "A"
                    elif grade >= 80:
                            finalGrade = "B"
                    elif grade >= 70:
                            finalGrade = "C"
                    elif grade >= 60:
                            finalGrade = "D"
                    else:
                            finalGrade = "F"

                    print("Your grade is " + finalGrade)
                    ans = (input("Would you like to calculate a grade for another class? "))
                    while(ans != "yes" and ans != "no"):
                            print("Please type yes or no")
                            ans = (input("Would you like to calculate a grade for another class? "))
    if ans == "no":
            exit()
main()

查看形成发送的右侧参数。

答案 1 :(得分:0)

如果您的表单没有直接绑定到对象模型,则可能很难使用form_for帮助程序。在这些情况下,您应该使用form_tag helper。阅读有关form_tag helper here的更多信息。

请参阅此stackoverflow答案,了解form_for和form_tag帮助程序之间的区别。

希望这有帮助。