views:

55

answers:

2

I created the following route:

  map.todo "todo/today",
            :controller => "todo",
            :action => "show_date"

Originally, the 'show_date' action and associated view would display all the activities associated for that day for all the Campaigns.

This ended up being very slow on the database...it would generate roughly 30 records but was still slow.

So, I'm thinking of creating a partial that would first list the campaigns separately.

If someone clicked on a link associated with campaign_id = 1, I want it to go to the following route:

todo/today/campaign/1

Then I would like to know how to know that the '1' is the campaign_id in the controller and then just do its thing.

The reason I want a distinct URL is so that I can cache this list. I have to keep going back to this and it's slow.

NOTE: It's possibly the problem actually is that I've written the queries in a slow way and sqlite isn't representative of how it will be in production, in which case this work-around is unnecessary, but right now, I need a way to get back to the whole list quickly.

A: 

Just with the following :

map.todo "todo/today/:id",
        :controller => "todo",
        :action => "show_date"

This will create the /todo/today/:id url where id is whatever you set in the url.
You can then access it in your controller with params[:id].

You might be interested in reading Rails Routing from the Outside In, particularly the section about resources.

Damien MATHIEU
thanks let me try this
Angela
+1  A: 

The code above by @Damien is correct but incomplete. It should be:

map.todo "todo/today/campaign/:id", :controller => "todo", :action => "show_date"

in your views all you have to do is:

<%= link_to "Campaign 1", todo_path(:id => 1) %>

or simply

<%= link_to "Campaign 1", todo_path(1) %>

and the particular campaign id can be fetched using params[:id] in your action.

And yeah, sqlite is not production ready.

EDIT: The latter part is quite easy to implement:

However, you have to change the route slightly, The route will now become,

map.todo "todo/today/:campaign/:id", :controller => "todo", :action => "show_date"

in your views:

<%= link_to "Campaign 1", todo_path(:campaign => "campaign", :id => 1) %>

In your todo controller, show_date action:

def show_date

#IF YOU ARE USING THIS REPEATEDLY IN LOTS OF DIFFERENT ACTIONS THEN A BETTER PLACE FOR THIS WOULD BE AS A HELPER IN application_controller.rb

 if params[:id].nil? && params[:campaign].nil?
    #DO SOMETHING WHEN BOTH ARE NIL,
 elsif params[:campaign]!="campaign"
    #DO SOMETHING WITH CAMPAIGN BEING SOMETHING OTHER THAN "CAMPAIGN"
 elsif params[:campain]=="campaign" && params[:id].nil?
    #DO SOMETHING WITH ID BEING NIL.
 else
    #FIND YOUR CAMPAIGN HERE.
 end
end

Hope this helps. :)

Shripad K
thanks...let me try this -- wondering if I should port to mysql first before taking any actions
Angela
I would suggest going for postgresql. If you have any plans of deploying your app on heroku that is.
Shripad K
yeah, taking it to heroku....this doesn't change this solution, does it?
Angela
No it doesn't. However, there is a "Common issues migrating to PostgreSQL" section in the Heroku docs. You could avoid those issues if you migrate to PostgreSQL and use it through out in development.
Shripad K
Thanks, checked it out....trying this -- lots of different partials so haven't been able to see if thsi ports cleanly all the way yet....but hopefully soon.
Angela
Is there a way to have a default so that if we go /todo/today/ it will not bonk out -- maybe it will show a default :id for the campaign?
Angela
I edited my answer above. :)
Shripad K
Hello Shripad, yep, this is pretty cool....!!
Angela
I +1'd for all the detail
Angela
Thanks. But i unfortunately made it a community wiki hoping others had a better solution. :)
Shripad K