Forum: Ruby on Rails radio buttons and whatnot

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.
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-13 13:28
Hey guys,

I got a statuses model which holds all my applications statuses, they
are differentiated by their status type.

In my application I have some invoices that I want to be able to set
their invoice.status using radio buttons (which are created in my view
from the statuses table).

Does anyone have any idea on how to achieve this, as there appears to be
little help available online for such things.

thanks a bunch

dave
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-13 14:54
Dave Smith wrote:
> Hey guys,
>
> I got a statuses model which holds all my applications statuses, they
> are differentiated by their status type.
>
> In my application I have some invoices that I want to be able to set
> their invoice.status using radio buttons (which are created in my view
> from the statuses table).
>
> Does anyone have any idea on how to achieve this, as there appears to be
> little help available online for such things.
>
> thanks a bunch
>
> dave

nay ideas?
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-01-13 17:55
Seek out the API docs grasshopper...

From  http://api.rubyonrails.org/

Module
ActionView::Helpers::FormHelper

radio_button(object_name, method, tag_value, options = {})

Returns a radio button tag for accessing a specified attribute
(identified by method) on an object assigned to the template (identified
by object). If the current value of method is tag_value the radio button
will be checked. Additional options on the input tag can be passed as a
hash with options.

Let's say that @post.category returns "rails":
  radio_button("post", "category", "rails")
  radio_button("post", "category", "java")

Or
  radio_button("user", "receive_newsletter", "yes")
  radio_button("user", "receive_newsletter", "no")
D188e591eac11021329b8821a5f954c7?d=identicon&s=25 Ar Chron (railsdog)
on 2009-01-13 18:00
In your case, have the appropriate controller read in the statuses info
and hand that to the view...

@statuses = Status.find(:all, :order => 'something')

Then the view (or a partial) can do something like:

@statuses.each do |this_status|
  radio_button("application", "status", this_status.value)
end

or sumfink like that...
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2009-01-13 18:26
(Received via mailing list)
Can't resist adding this gratuitous and unsolicited advice:

If an application can only have one status at a time, resist any urge
you might feel to store status in a series of boolean fields in
applications.  Instead, have your controller just store the id of the
selected status in application.status_id.

That way you don't have to worry that an application might wind up with
>1 of those booleans checked at a time, you should be able to add/rename
statuses at will, etc.
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-13 18:52
Ar Chron wrote:
> In your case, have the appropriate controller read in the statuses info
> and hand that to the view...
>
> @statuses = Status.find(:all, :order => 'something')
>
> Then the view (or a partial) can do something like:
>
> @statuses.each do |this_status|
>   radio_button("application", "status", this_status.value)
> end
>
> or sumfink like that...

brill... i understand.

what i have done it put it in a partial in my page form.

I have a projects page with a list of project_tasks and I want to use
the same code in this page and have the status in the form of a radio
button for each task, so I have put the code into a partial in projects
view.

This is my partial.

<% Status.find_all_by_type("Project").each do |s|%>
    <%= f.radio_button :status_id, s.id %>
    <%= f.label :status_id, s.name %>
<% end %>


I am trying to make a link to the partial on the projects show.html.erb
page. I know the code below this will not work, but am trying to adapt
it to take the task instead of the project form object.

    <%= render :partial => "statuses", :locals => { :f => f } %>

obviously my partial here will be expecting a project form object.. any
clues?

<% form_for(@project) do |f| %>
    <% for task in @project_tasks %>
    <%= render :partial => "statuses", :locals => { :f => f } %>
    <% end %>
<% end %>

I want it to take the task object instead

any ideas?
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-14 12:25
> <% form_for(@project) do |f| %>
>     <% for task in @project_tasks %>
>     <%= render :partial => "statuses", :locals => { :f => f } %>
>     <% end %>
> <% end %>
>
> I want it to take the task object instead
>
> any ideas?

bump
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-01-14 12:45
Dave Smith wrote:
>
>> <% form_for(@project) do |f| %>
>>     <% for task in @project_tasks %>
>>     <%= render :partial => "statuses", :locals => { :f => f } %>
>>     <% end %>
>> <% end %>
>>
>> I want it to take the task object instead
>>
>> any ideas?
>
> bump

dont worry i fixed it. just moved the form into the loop cheers guys
This topic is locked and can not be replied to.