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
It shouldn’t be case-sensitive.
User.find_by_username(params[:user_id]) should return the user.
[email protected] 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
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.
On 2007-10-27 04:28:05 -0700, Mix M.
[email protected] 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.
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.
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 => :indexI 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
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.
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