使用Rails 3.2和Rails表单 – 当选中单选按钮时,不知道如何显示输入文本字段,否则隐藏它

我在这里找了几个post,但我仍然无法使它工作。 当选中“其他”单选按钮时,我希望显示f.text_field以及何时取消选中以隐藏它。 之后我希望能够为thr属性f.client_consent_refusal_reason输入文本值。

这是我的HTML:

{:controller => 'client', :action=> :outcome_consent_complete, :id => @client.id }) do |f| %> false %>
false %>
false %>
false %>
false %>

当检查上述任何单选按钮时,应将字段client_consent_refusal_reason设置为关联值,但是当选中Other时,我希望能够捕获对应的值。

我不知道如何解决这个问题。

我替换了这部分代码:

 <%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false %> <%= f.label :radio_button_label, 'Other' %> <%= f.text_field :client_consent_refusal_reason %> 

有了这个:

 <%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false, :onchange => "$('other_reason_input')[this.checked ? 'show' : 'hide']();" %> <%= f.label :radio_button_label, 'Other (please list): ' %>  

在模型类中,我必须添加以下行:

  attr_accessor :client_consent_refusal_reason_other def client_consent_refusal_reason_other=(value) self.client_consent_refusal_reason = value if client_consent_refusal_reason == 'Other' end def client_consent_refusal_reason_other client_consent_refusal_reason end 

现在一切都按预期工作了。