I have a controller streams_controller and a model streams.
class StreamsController < ApplicationController
def new
@stream = Stream.new
end
def create
@stream = Stream.new(params[:stream])
if @stream.save
flash[:success] = "Welcome!"
redirect_to @stream
else
render 'new'
end
end
def show
@stream = Stream.find(params[:id])
end
end
and
class Stream < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
before_save { |stream| stream.name = name.downcase }
has_secure_password
.
.
.
Basically to work the show method, I have to go localhost/streams/[id] where [id] is the id of the particular Stream. Would it be possible to reroute the URL to go something like this: localhost/[name] where [name] is the :name attribute of the Stream model?
So basically a new URL would be created every time a new Stream is created, and it would correspond to the name of the Stream in the database.
How would I go about implementing this?
Anyways, any help or thoughts greatly appreciated!
I would make the
streamspart of the url as short as possible, but not empty (to avoid collisions in the case of using just names instead of ids).Url
/s/stream-aisn’t that big compromise.About using name instead of id in your url see this Railscast.