I have a simple quick question. I have a Url:
http://www.mealnut.com/foodios/cakerystore
Need to make it as
http://www.mealnut.com/cakerystore
Can anybody tell how to do this? I am new to rails.
I have a simple quick question. I have a Url:
http://www.mealnut.com/foodios/cakerystore
Need to make it as
http://www.mealnut.com/cakerystore
Can anybody tell how to do this? I am new to rails.
Your question is still very unclear, but I think you need a catch-all rule in your routes.rb:
get "*username" => "foodios#user_action"
This will allow you to have root urls for each user, which is what I think you're really asking for:
http://www.mealnut.com/cakerystore
http://www.mealnut.com/bakeland
http://www.mealnut.com/cookieworld
This should be last route defined in your routes.rb file. That's because this route will match all URLs not matched by other routes defined earlier in the file. (Rails attempts to match routes in the order that they are defined.)
This route directs the url to the FoodiosController::user_action
action (e.g. method). (You can rename user_action
, of course.)
This action would be defined something like this:
# In FoodiosController
def user_action
# params[:username] comes from '*username' in the route
@user = User::find_by_username(params[:username])
unless @user.nil?
# not sure what you want to show, but this would do user#show
render @user
# or you can render some other path
else
# redirect to some error page
end
end
Because this route catches everything, that else case in there will need to do something appropriate for not just invalid usernames, but any kind of bad URL you may get. It's all controlled by this action now.
For what it's worth, I think you'd be better off not doing it this way. It sounds like you should have a UsersController, and your URL should be http://www.mealnut.com/users/cakerystore
. It would be so much easier.
Credit/further reading:
I doubt that this is the answer to your problem. But it is the answer to your question.
'http://www.mealnut.com/foodios/cakerystore'.gsub(/\/foodios/,'')