Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6676487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:59:39+00:00 2026-05-26T03:59:39+00:00

I’m in the process of learning RoR, and I’m liking everything that I’m discovering

  • 0

I’m in the process of learning RoR, and I’m liking everything that I’m discovering so far. I’m switching over from the PHP-based CodeIgniter framework, and I have a problem in using the redirect_to method.

I’ve defined a basic Users model which I use to handle registration – data gets stored fine in the DB, but the problem is when redirecting after signing up a user to the system.

Basically, the profile page is of the following format: /users/:name/:id

I have a routes file defined as such:

resources :users
match '/users/:name/:id', :to => 'users#show'

And here is my create method

def create
  @title = "User creation"

  @user = User.new(params[:user])
  if @user.save
    info = { :name => @user.name, :id => @user.id }

    redirect_to info.merge(:action => "show")
  else
    @title = 'Sign Up'
    render 'new'
  end
end

However this will generate an url of the following format:

http://localhost:3000/users/27?name=Testing

When I’m actually looking for something like this:

http://localhost:3000/users/Testing/27

It just makes sense for me from a SEO point of view that a profile page URL look like that. I’ve been searching the inter-webs, but I only find solutions to different problems. I hope someone can help.

SOLVED
Both versions that were suggested by Ryan worked fine, and I decided to stick to the second one, since it feels more RESTful. I’ll just share the config I have right now – mind you, the User model may not be all that correct, but it’s the to_param function that’s important. Also, I’ve noticed that it doesn’t work if I make the function private – that makes sense, but I just thought that I’d share that for someone that may be running into that sort of problem.

Here’s my routes file:

resources :users

And here is my Users model:

class User < ActiveRecord::Base
  attr_accessible :name, :email

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, 
      :presence => true,
      :length => { :within => 5..50 }

  validates :email,
      :presence => true,
      :format => { :with => email_regex},
      :uniqueness => { :case_sensitive => false }

  def to_param
    "#{id}-#{name.parameterize}"
  end
end

And here’s my controller create function:

def create
  @title = "User creation"

  @user = User.new(params[:user])
  if @user.save
    redirect_to @user
  else
    @title = 'Sign Up'
    render 'new'
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:59:40+00:00Added an answer on May 26, 2026 at 3:59 am

    Define your route like this:

    get '/users/:name/:id', :to => 'users#show', :as => "user"
    

    Then redirect to it using this helper:

    redirect_to(user_path(@user.name, @user.id))
    

    Alternatively, you could just stick with resources :users and not have to define your own route. The difference here is that your route would be something like /users/1-testing rather than users/1/testing, but the advantage is that you would be more Rails standard.

    To do this, define a to_param method in your model, like this:

    def to_param
      "#{id}-#{name.parameterize}
    end
    

    Then Rails will use the output of the to_param method in your routes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.