Greetings, I have a partial form that displays a questionnaire. Each question has a group of radio buttons associated with it which allow the user to select only one possible answer. In order to do this, I am using a combination of ''form_for'' and ''radio_button'' as shown below. My problem is, "how do I dynamically pass a value to one of the radio buttons so that it is checked when the form first renders? Below is a snippet of the code I am using in the partial form. <% form_for :questionnaire, @questionnaire do |f| %> My question is bla, bla, bla? <%= f.radio_button :field_name, "Y" %>The answer is yes. <%= f.radio_button :field_name, "N" %>The answer is no. <% end %> Thanks, Doug -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Follow-up Question: How does one populate the radio button values from an array of stored values? For example, I have a form containing questions and answers associated with radio buttons. The values selected (by radio button) get stored in a database. Later, someone wants to change the answers. How do we go about setting the radio button values in the edit form? Going back to my previous code example: <% form_for :questionnaire, @questionnaire do |f| %> My question is bla, bla, bla? <%= f.radio_button :field_name, "Y" %>The answer is yes. <%= f.radio_button :field_name, "N" %>The answer is no. <% end %> The above code produces the following HTML: <form action="/questionnaire" method="post"> My question is bla, bla, bla? <input id="questionnaire_field_name_y" name="questionnaire[field_name]" type="radio" value="Y" />Yes <input id="questionnaire_field_name_n" name="questionnaire[field_name]" type="radio" value="N" />No The above works fine when I submit and save the data. However, when I later try to retrieve and edit the data, I have problems. Specifically, when I try to pass the ''edit'' form an instance variable containing the questionnaire hash: @questionnaire = {:field_name => "Y"} I get an error message like this: undefined method `field_name'' for {"field_name"=>"Y"}:HashWithIndifferentAccess Any help would be greatly appreciated. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 7 Nov 2007, at 02:16, Doug Meharry wrote:> > > The above works fine when I submit and save the data. However, when I > later try to retrieve and edit the data, I have problems. > Specifically, > when I try to pass the ''edit'' form an instance variable containing the > questionnaire hash: @questionnaire = {:field_name => "Y"} > > I get an error message like this: undefined method `field_name'' for > {"field_name"=>"Y"}:HashWithIndifferentAccess >The form_for helpers don''t work with a hash: they expect the thing they are editing to have accessor methods for the attributes you are editing. Fred> Any help would be greatly appreciated. > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 6, 2007 3:28 PM, Doug Meharry <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Greetings, > > I have a partial form that displays a questionnaire. Each question has > a group of radio buttons associated with it which allow the user to > select only one possible answer. In order to do this, I am using a > combination of ''form_for'' and ''radio_button'' as shown below. My problem > is, "how do I dynamically pass a value to one of the radio buttons so > that it is checked when the form first renders? Below is a snippet of > the code I am using in the partial form. > > <% form_for :questionnaire, @questionnaire do |f| %> > My question is bla, bla, bla? > <%= f.radio_button :field_name, "Y" %>The answer is yes. > <%= f.radio_button :field_name, "N" %>The answer is no. > <% end %>That should do it. The radio_button helper will mark the appropriate button based on the value of @questionnaire.field_name. The first will be marked if the value is "Y" and the second will be marked if the value is "N". To get a default value, assign it when you create @questionnaire in your controller: @questionnaire = Questionnaire.new(:field_name => "N") --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob Showalter wrote:> On Nov 6, 2007 3:28 PM, Doug Meharry <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> >> <% form_for :questionnaire, @questionnaire do |f| %> >> My question is bla, bla, bla? >> <%= f.radio_button :field_name, "Y" %>The answer is yes. >> <%= f.radio_button :field_name, "N" %>The answer is no. >> <% end %> > > That should do it. The radio_button helper will mark the appropriate > button based on the value of @questionnaire.field_name. The first will > be marked if the value is "Y" and the second will be marked if the > value is "N". To get a default value, assign it when you create > @questionnaire in your controller: > > @questionnaire = Questionnaire.new(:field_name => "N")Thanks Bob, this fixed the problem. Doug -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---