0

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.

user2206724
  • 1,265
  • 3
  • 20
  • 38

2 Answers2

1

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
  • etc

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:

Community
  • 1
  • 1
Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
0

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/,'')
wintermeyer
  • 8,178
  • 8
  • 39
  • 85
  • not working. I am doing this: <%= link_to "by #{product.user.profile.brand_name}", "/foodios/#{p.user.profile.brand_name}", :class => "p_foodio" %> to get above url. – user2206724 May 11 '13 at 06:33
  • Why do you not just use: `<%= link_to "by #{product.user.profile.brand_name}", "#{p.user.profile.brand_name}", :class => "p_foodio" %>` to get rid of the `foodios` part? – wintermeyer May 11 '13 at 12:35
  • Or did you mean: "How can I configure my Rails app to connect http://www.mealnut.com/foodios/cakerystore with http://www.mealnut.com/cakerystore?" That would be a routing task. Have a look at http://xyzpub.com/en/ruby-on-rails/3.2/routes.html – wintermeyer May 11 '13 at 12:37