# Case sensitive url and link\_to... help

**URL:** <https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471>\
**Category:** Rails\
**Created:** [October 27, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471 "2007-10-27T13:28:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 27, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/1 "2007-10-27T13:28:05Z")

</div>

Hi, after saying this you will hate me, but i’ve changed an url which  
takes not the id but the nick. Something like /users/mix/comments  
everything works fine, just for a “little” problem, when i create the  
link with link\_to or the paginate method it lowercase the controller  
string, so if i pass it something like :controller =\>  
“/users/#{@user.nick}/comments”, :action =\> :index it will lowercase the  
nick, and if the nick is eg MiX it won’t find any user with mix.  
How can i solve this with have the nick as is? with link\_to it can be  
solved without pass {:controller… } but  
“/users/#{@user.nick}/comments”, it works, but the problem stays with  
the paginate url, it doesn’t accept that  
Or another solution would be accept any kind of nick case insensitive,  
but i think that this would charge more the search in the db, and anyway  
it’s better to show the nick as the user has written it  
Thanks

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 27, 2007, 4:35pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/2 "2007-10-27T16:35:42Z")

</div>

It shouldn’t be case-sensitive.

User.find\_by\_username(params[:user\_id]) should return the user.

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 27, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/3 "2007-10-27T17:02:11Z")

</div>

removed\_email\_address@domain.invalid wrote:

> It shouldn’t be case-sensitive.
> 
> User.find\_by\_username(params[:user\_id]) should return the user.

the problem is that find\_by\_username is case sensitive, if i’ve a user  
“Mix”, if i find\_by\_username(‘mix’) it won’t be found

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 27, 2007, 6:48pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/4 "2007-10-27T18:48:24Z")

</div>

I know this isn’t exactly what you want, but I find it a nice go  
between often times, and you don’t need to hack everything up to make  
it work.

I would pass the id as well as the username. You can do that by  
setting the method to\_param in the model file for whatever model you  
want to preform this on, in your case, the user model.

# user.rb

def to\_param  
“#{id}-#{username}”  
end

The cool thing about to\_param, is that you don’t have to change any of  
your link\_to methods, or .find methods normally.

If you did this in the view:

# index.rhtml

link\_to @user.username, {:action =\> “show”, :id =\> @user}

or if you are using restful routes:  
link\_to @user.username, user\_path(@user)

Looks pretty normal right? The cool thing is that even though it  
looks that normal, when you hit the link in the browser, the url will  
look like this:  
“/users/12-billybob”

And in your controllers, you can do a very basic:  
User.find(params[:id])

If you still want to use only the username, there are also ways to do  
that, but they are not as easy, lemme know.

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 28, 2007, 1:42am UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/5 "2007-10-28T01:42:39Z")

</div>

On 2007-10-27 04:28:05 -0700, Mix M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> “/users/#{@user.nick}/comments”, it works, but the problem stays with  
> the paginate url, it doesn’t accept that  
> Or another solution would be accept any kind of nick case insensitive,  
> but i think that this would charge more the search in the db, and anyway  
> it’s better to show the nick as the user has written it  
> Thanks

Why are you doing this:

> :controller =\> “/users/#{@user.nick}/comments”,  
> :action =\> :index

I don’t understand… you have a controller (or namespace) named for  
each user!? I suspect that’s just an error in your question…

I’m surprised that it would be changing the case of your user.nick, but  
if there is in fact a problem with that, you could write your own  
find\_by\_insensitive\_nick method to work around that.

Have you tried using to\_param for this?

class User  
def to\_param  
nick  
end  
end

Then, if you’re using map.resources for the route:

\<%= link\_to user\_comments\_path(@user) %\>

Or else:

\<%= link\_to :controller =\> ‘comments’, :user\_id =\> @user, :action =\>  
‘index’ %\>

HTH,  
–Andrew V.

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 27, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/6 "2007-10-27T18:22:40Z")

</div>

I know this isn’t exactly what you want, but I find it a nice go  
between often times, and you don’t need to hack everything up to make  
it work.

I would pass the id as well as the username. You can do that by  
setting the method to\_param in the model file for whatever model you  
want to preform this on, in your case, the user model.

# user.rb

def to\_param  
“#{id}-#{username}”  
end

The cool thing about to\_param, is that you don’t have to change any of  
your link\_to methods, or .find methods normally.

If you did this in the view:

# index.rhtml

link\_to @user.username, {:action =\> “show”, :id =\> @user}

or if you are using restful routes:  
link\_to @user.username, user\_path(@user)

Looks pretty normal right? The cool thing is that even though it  
looks that normal, when you hit the link in the browser, the url will  
look like this:  
“/users/12-billybob”

And in your controllers, you can do a very basic:  
User.find(params[:id])

If you still want to use only the username, there are also ways to do  
that, but they are not as easy, lemme know.

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 28, 2007, 4:57pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/7 "2007-10-28T16:57:17Z")

</div>

Matthew Swasey wrote:

> If you still want to use only the username, there are also ways to do  
> that, but they are not as easy, lemme know.

Yep, i need to have just the username without the id ☹

Andrew V. wrote:

> Why are you doing this:
> 
> > :controller =\> “/users/#{@user.nick}/comments”,  
> > :action =\> :index
> 
> I don’t understand… you have a controller (or namespace) named for  
> each user!? I suspect that’s just an error in your question…

Because i’ve it to show all the comments the user have written, then  
i’ve a /comments/id to show just that comment and a /product/id/comments  
to show all the product’s comments. I think it’s quite strange 🙂

> I’m surprised that it would be changing the case of your user.nick, but  
> if there is in fact a problem with that, you could write your own  
> find\_by\_insensitive\_nick method to work around that.

Sorry the ignorance, but how can i search with a case insensitive? and  
with, is it possible that the performances will be lower, right ?

> Have you tried using to\_param for this?

yes, but doesn’t work ☹

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [November 10, 2007, 9:57am UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/8 "2007-11-10T09:57:10Z")

</div>

If you use MySQL you can simply set the collation of the username  
field to utf8\_general\_ci (ci = case insensitive) for the same result.

---

<div class="post-metadata">

**Author:** ![mix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@mix](https://www.ruby-forum.com/u/mix)\
**Post date:** [October 28, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/case-sensitive-url-and-link-to-help/118471/9 "2007-10-28T17:41:49Z")

</div>

Solved, i simply use ferret, i’ve done a find\_by\_username which use  
ferret with an insensitive case search, and return the result.  
Everything now works perfectly 🙂
