+'s in urls?

Okay so right now I have this as a route:

map.connect ‘browse/:cal_name’, :controller => ‘browse’, :action =>
‘events_listing’

If the :cal_name has spaces in it (Test Calendar) for example, then the
url appears as Test+Calendar in the url. Is there a way to substitute
something else (say ‘-’ or something) for the ‘+’ in the url?

Thanks for your help :slight_smile:

Okay so right now I have this as a route:

map.connect ‘browse/:cal_name’, :controller => ‘browse’, :action =>
‘events_listing’

If the :cal_name has spaces in it (Test Calendar) for example, then the
url appears as Test+Calendar in the url. Is there a way to substitute
something else (say ‘-’ or something) for the ‘+’ in the url?

I’d mix things up a little bit. I’m going to assume some things about
your model names, but that’s not the important part :slight_smile:

class Calendar < ActiveRecord::Base

assuming the following columns: id:integer, name:string

def to_param
“#{id}-#{name.gsub(/[^a-z0-9]+/i, ‘-’).downcase}”
rescue
id
end

end

Then change your route to this:

map.browse_calendar ‘browse/:calendar’, :controller => ‘browse’, :action
=> ‘events_listing’

Then in your controller do this:

@calendar = Calendar.find(123) # assuming id=123 is ‘Test Calendar’

And in your view do this:

<%= link_to ‘Browse’, browse_calendar_path(:calendar => @calendar) %>

That should result in a URL such as:

/browse/123-test-calendar

If for some reason doing the substition in that to_param() method fails
you will get:

/browse/123

In either case you can then use that information in your events_listing
action to retrive the calendar using:

Calendar.find_by_id(params[:calendar])

You could also change the to_param() method to not put the “123-” at the
beginning, but would then need to tweak your finding later on to look
for either an id or a calendar name.

Hope this helps,

-philip

Is there a way to just alter the :cal_name (i.e. gsub(’ ', ‘-’)) or
something in the route? I’m a newbie so bare with me…this is probably
completely obvious ><

Is there a way to just alter the :cal_name (i.e. gsub(’ ', ‘-’)) or
something in the route? I’m a newbie so bare with me…this is probably
completely obvious ><

Yes.

Exactly what you have above will work…

Philip H. wrote:

Is there a way to just alter the :cal_name (i.e. gsub(’ ', ‘-’)) or
something in the route? I’m a newbie so bare with me…this is probably
completely obvious ><

Yes.

Exactly what you have above will work…

but how do I get it to do the gsub in the middle of a string?

<%= link_to ‘browse’, {:action => ‘event_listing’, :cal_name => “Test
Calendar”.gsub(’ ', ‘-’)} %>

-philip

Philip H. wrote:

Is there a way to just alter the :cal_name (i.e. gsub(’ ', ‘-’)) or
something in the route? I’m a newbie so bare with me…this is probably
completely obvious ><

Yes.

Exactly what you have above will work…

but how do I get it to do the gsub in the middle of a string?

Amanda … wrote:

doing “browse/#{:calname.gsub(’ ', ‘-’)}” doesn’t work

haha bad timing, okay i’ll try that

Philip H. wrote:

<%= link_to ‘browse’, {:action => ‘event_listing’, :cal_name => “Test
Calendar”.gsub(’ ', ‘-’)} %>

-philip

Okay the problem with this is that the actual :cal_name with spaces is
really important in my code…maybe I should just add another parameter?

Amanda … wrote:

Okay the problem with this is that the actual :cal_name with spaces is
really important in my code…maybe I should just add another parameter?

Nevermind if I add another parameter, it just puts :cal_name on the end
of it lol…is there a way to use gsub right in

map.connect ‘browse/:cal_name’, :controller => ‘browse’, :action =>
‘events_listing’?

doing “browse/#{:calname.gsub(’ ', ‘-’)}” doesn’t work

Okay figured out a way to do it…completely inefficient though :slight_smile: