Question

I am extremely "green" when t comes to Ruby & Rails, I could do this easily in VB, but I want to learn rails....

I have a simple index view which shows all the clients in my database, and I figured out how to replace the index action in the controller to display only the clients who are marked as "active" (a field in the clients table), but my head is swimming trying to figure out routing and/or control actions for switching between the two or three recordsets (in VB terms).

My ultimate goal would be to have radio buttons on the index view where the user chooses between "active", "inactive" or "all".

Currently I have this in the clients controller...

@clients = Client.find(:all, :conditions => { :active => true })
#@clients = Client.find(:all, :conditions => { :active => false })
#@clients = Client.all.order(sort_column + ' ' + sort_direction)

If I comment out two of the lines, the remaining one does exactly as I want.

4 specific questions: (1) How do I write a conditional statement to make this switching occur, (2) WHERE should this be implemented, controller? Routing? Elsewhere?, (3) can this be implemented with user selectable radio buttons in the index view?, and (4) how do I add my "order" condition back in. (I tried just daisy-chaining it onto the end, but that doesn't work.)

Thanks in advance, MDS

Was it helpful?

Solution 2

You can have a dropdown in your index view that has all 3 statuses (radio buttons probably aren't that different but I don't have an example right now):

<%= form_tag("/clients", method: "get") do %>
  <%= select_tag(:active, options_for_select([['Active', true], ['Inactive', false], ['All', '']], params[:active]), :prompt => 'Status') %>
  <%= submit_tag("Search") %>
<% end %>

(Assuming your controller index page is at /clients, if not change the value in the form_tag)

You then add a scope to your Client model that takes an argument, this way you're not cluttering up your controller:

scope :active, lambda { |active| where(:active => active)}

In your controller, you then call the scope with the param value if it's set:

@clients = Client.all.order(sort_column + ' ' + sort_direction)
@clients = @clients.active(params[:active]) unless params[:active].blank?

No need to mess with routing since you're just passing a params value.

OTHER TIPS

You've got a couple of strategies.

1) Load all the records onto the page, give them a class based on their active status, and hide some of them. The radio buttons have javascript attached, which shows or hides records with a particular class.

2) Load one set of records, and make the radio buttons trigger a request to the server to reload the contents of the list, passing through different params, eg "active=true" or "active=false" (when params[:active] isn't present you could load all of them). It's nicer if this is done via ajax but you could have it reload the whole page.

I wouldn't do this via routing since it's just a params thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top