views:

12

answers:

1

This is likely something easy to accomplish, but I'm having difficulty even articulating clearly, leading me to get all sorts of semi-germaine, but not quite what I'm after posts on SO when searching.

I have a resource, we'll just use User as a simple to discuss use case. I desire SEO friendly URLs, so instead of ~/users/:id, we have ~/users/:name, where the show action on my users controllers is doing a User.find_by_name(:name). Works great.

In various parts of the app, I want to redirect the end user back to my resource. The docs seem to indicate I should do

redirect_to @user, :notice => "your page is now DIAMONDS!"'

The problem here is that this automatically appears to use the :id value of the resource, which isn't what I'm after. I'm wondering if there's a way to just define a route in my routes file which is aware of my desire to use the name property--and then just redirect to the generated route_url helper. But I'm at a loss for how to do that.

In the interim, I'm resorting to this:

flash[:notice]    = "Your account is now DIAMONDS.  *fist bump*"
redirect_to :action => :show , :id => @user.name

This is less than ideal to me as it's a good bit of repeated code (I have lots of UI elements that will be linking back to my resource) and because I can't seem to figure out how to include the flash as part of the redirect_to method call---every combo of curly bracing w/in the redirect_to that includes a flash bombs on me.

I don't think it's germaine to my problem specifically, but I am doing this on Rails 3 in case it does have some implication in terms of options available to me.

Sorry for the noobishness :)

A: 

Pretty simple to do. The standardish way of doing this is:

/app/models/user.rb

def to_param
  "#{id}-#{name}"
end

That's nice, and gives you nicer SEO -- http://yoursite.com/users/1345-supercool .... You can tweak it a bit to remove the '1345':

/app/models/user.rb

  # add_column :permalink, :string
  attr_protected :permalink
  before_create :set_permalink
  validates_uniqueness_of :permalink

  def set_permalink
    self.permalink = username.parameterize
  end

  def to_param
    permalink
  end

This will give you http://yoursite.com/users/supercool and will be a permanent URL, so that if the user changes their username later, the URL will stay the same and keep search engines happy.

Jesse Wolgamott
The url design's a bit out of my jurisdiction, but if I were to go to this design or stick with mine, I'm still not grokking the easy way to generate links to the desired pattern, since the generated route helpers (eg. my_resource_path|url) all assume the ID field--same with doing something like redirect_to @resource
bakasan
see http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001840 for the api explanation of to_param. it's magic. :)
DGM
strangely, it is not working for me now. :(
DGM
DING DING! Awesome, the combo of DGM + Jesse's answer was precisely what I was after--me being the rookie that I am (literally 2 days into Ruby + RoR) I didn't fully understand what Jesse was getting at w/ his answer till DGM chimed in. Quick swap and works exactly like we wanted. :)
bakasan