I am making a survey app with possible answers between 1 -5 agree/disagree.

If the user submits the form and has not answered all questions I redirect them back, and am trying to set the correct radio button as checked so they do not lose their existing answers.

i.e if for question 5 they selected 4 as their answer, then I want to set its value to 4.

The problem is the code below is not setting checked='checked' when it should.

input id="#{ question.id }-5" name=question.id type='radio' value='5' checked=(params[question.id.to_i.to_s.to_sym] == 5?'checked':false)
有帮助吗?

解决方案

Try this:

 checked=(params[question.id.to_s] == '5'?'checked':false)

The main change is comparing params value to String '5' and not Integer 5 as the params value is a string itself.

Also you do not need to to_i.to_s.to_sym , to_s should suffice but that was not the issue.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top