Forum: Ruby on Rails Radio button issues in a list

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
673ef77e1bb2706865be3fa9cd00c7e3?d=identicon&s=25 Teresa Mcmillin (tmac26)
on 2008-10-16 01:01
Hi,

I have a list of companies for a given user.  One (and only one) company
can be a default company for that user.  I have a radio button that
appears on each row in the list of companies (here is my code):

<% form_tag :action => 'update_default_company', :id => @company,
:method => :post do %>
  <table>
    <tr>
    <th><%= "Name" %></th>
    <th><%= "Size" %></th>
    <th><%= "Default Company" %></th>
    </tr>

  <% for @company in @companies %>
    <tr>
      <td><%= @company.name %></td>
    <td><%= @company.size %></td>
    <td><%= radio_button("company", "default_company", true) %>
</tr>
  <% end %>
  </table>
  <%= submit_tag "Change Default" %>
<% end %>

From this list, the user is allowed to change the default company and
click on a button (implemented with a submit tag, which calls a method
in the controller).  The radio buttons are the only editable field on
this form.

Question:  How do I get the necessary values back to the controller?
Currently, I only get the selected radio button, but not the associated
company data.

TIA,

TMac
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-16 01:43
(Received via mailing list)
On Oct 16, 12:01 am, Teresa Mcmillin <rails-mailing-l...@andreas-
s.net> wrote:
>     <td><%= radio_button("company", "default_company", true) %>

that third parameter is what will appear in params[:company]
[:default_company] so typically you set it to something like
@company.id.

Overwriting @company like that won't do you any favours, I would do

 <% for company in @companies %>
    <tr>
      <td><%= company.name %></td>
    <td><%= company.size %></td>
    <td><%= radio_button("company", "default_company", company.id) %>
</tr>
<% end %>

Fred
673ef77e1bb2706865be3fa9cd00c7e3?d=identicon&s=25 Teresa Mcmillin (tmac26)
on 2008-10-16 02:31
Frederick Cheung wrote:
> that third parameter is what will appear in params[:company]
> [:default_company] so typically you set it to something like
> @company.id.
>
> Overwriting @company like that won't do you any favours, I would do
>
>  <% for company in @companies %>
>     <tr>
>       <td><%= company.name %></td>
>     <td><%= company.size %></td>
>     <td><%= radio_button("company", "default_company", company.id) %>
> </tr>
> <% end %>
>
> Fred

I have changed the third parm and I see what you mean, but here is my
confusion.  I thought the third parm was used for determining which
radio
button should be selected, when loading the companies from the database.
In my case, only one company has default set to true, so I put true in
the third parm.  It works nicely for displaying the data.

So now the question becomes how do I set that radio button values to
correspond to the database values?

Thanks for your help.

TMac
This topic is locked and can not be replied to.