2

I am new to rails and I would like to know that if there is a way to change the name of the routes. I will explain the sentence in a minute. I have a controller named users_controller and below is the code that I have in my controller:

def create
  if @user.save
  redirect_to @user
end

And my routes.rb file is as below:

resources :users

get '/users/:id', to: 'users#show'

Now suppose I have a form and I fill it correctly then I will be redirected to localhost:3000/users/id(id = some value) which I do. Now I want this URL to be like localhost:3000/registered . That is when i click the submit button and if everything is correct I wanted to see the URL as localhost:3000/registered which takes me to my show action or which works just like localhost:3000/users/id(id = some value). I don't want the ID to be visible.

For this I tried get 'registered', to: 'users#show' but still I see the same URL as localhost:3000/users/id(id = some value). Please tell me how to do this.

And here is my show action

def show

@user = User.find(params[:id])

end

I tried another way by making another action into the same controller. I made index action in my users_controller and executed then I got a No method Error

chaitanya90
  • 697
  • 2
  • 8
  • 24

3 Answers3

5

Just use your routing as:

match "/registered", :to => "users#show", :as => :user_registered, via: :get

to use this path you will use

redirect_to user_registered_path(id: user_id) 

in your method.

Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
Babar
  • 1,202
  • 1
  • 12
  • 21
2

At last, from the discussions I had it has been cleared that there is no way to hide your ID, but we can change the way they appear on the browser. One and suggested way is to create a hash for the ID and use it to display the item in show action or the other way is to redirect your action to some static page.

chaitanya90
  • 697
  • 2
  • 8
  • 24
1

You need to change your redirect_to in your user controller's create and update actions.

class UsersController < ApplicationController
  def create
     # get user from params
    if @user.save
      redirect_to registered_path
    else
      render :new
    end
  end

  def update
     # get user from params
    if @user.save
      redirect_to registered_path
    else
      render :edit
    end
  end
end

EDIT: As we discussed in the comments, you don't want to use "users#show", since the registered page will look the same for every user. You can make your own registered controller to do this.

rails g controller registered index
Joe Kennedy
  • 9,365
  • 7
  • 41
  • 55
  • Then it gives me an error that the page you are looking doesnot exists while running it on heroku – chaitanya90 Apr 29 '14 at 04:34
  • It says could not find an User without an ID – chaitanya90 Apr 29 '14 at 04:35
  • I think it has something to do with my Show action – chaitanya90 Apr 29 '14 at 04:37
  • I see the issue. The problem is that you're mapping `registered` to `"user#show"`, which expects an id for the user. Do you want to make a boiler plate registered page for all users, or are you looking to make a registered page that's unique for each user? – Joe Kennedy Apr 29 '14 at 04:39
  • I want all users to see the same show page just with their names in it when they log in. You can suppose it as a Thank you page for some site. – chaitanya90 Apr 29 '14 at 04:42
  • If user#show expects an ID then should I try changing the the name of action from sho to something different? – chaitanya90 Apr 29 '14 at 04:45
  • Yes. Just make your own action. `rails g controller registered index` should get everything you need to get started on your registered/thank you page. – Joe Kennedy Apr 29 '14 at 04:47
  • i tried creating a new action called index in my users_controller and then i made a call and I got No method error. Ihave updated my question. please have a look at it – chaitanya90 Apr 29 '14 at 05:51
  • You need to make the `index` action in the **RegisteredController**, not the UsersController. Or, you need to `get "registered", to "users#index"`. – Joe Kennedy Apr 29 '14 at 05:54
  • I did both. lets try it step by step. I HAVE MADE INDEX IN REGISTERED_CONTROLLER – chaitanya90 Apr 29 '14 at 05:55
  • I get an error saying Undefined local variable register_path in UsersController – chaitanya90 Apr 29 '14 at 05:56
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/51642/discussion-between-jken13579-and-ror) – Joe Kennedy Apr 29 '14 at 05:58