3

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)
4

1 回答 1

8

尝试这个:

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

主要变化是将参数值与String '5'而不是Integer 5因为参数值是字符串本身进行比较。

你也不需要to_i.to_s.to_symto_s应该就足够了,但这不是问题。

于 2013-10-14T08:19:02.283 回答