Question

How does the following piece of code redirect_to @userin my create action know how to redirect to my show view?

I don't get how it would possibly know this. I know this is a newb question but I just can't understand why @user works. It would make sense if it was something like redirect 'show' but why is @user used?

Here is my controller code:

class UsersController < ApplicationController

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

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)   
    if @user.save
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end
end
Was it helpful?

Solution

redirect_to @user

is the short hand for:

redirect_to user_path(@user)

You can either use the long-hand or the short-hand. They will both work the same way.

by convention, redirect_to @user maps to:

GET  /users/:id(.:format)    users#show 

That's how it knows to redirect you to the show page.

The show action is the only one (to my knowledge) that has a short-cut.

If you do rake routes, you'll see the complete map:

user_path    GET     /users/:id(.:format)    users#show

OTHER TIPS

If you run rake routes, you will able to see this route which is referring to users#show

GET /users/:id  users#show

In your case redirect_to @user which is equivalent of

redirect_to :action => 'show', :id => @user which matches the /users/:id convention

OR

It can also written as redirect_to user_path(@user) which also returns /users/:id

So,the controller just ends up redirecting to the show page.

These Guides will help you understand in detail.

Hope it helps!

Infact in syntax

redirect_to @user

@user variable here gives you the id of that user and if you see your routes (using rake routes) you would see a route users/:id which exactly maps to users#show. So that why it goes to show method or users controller.

You can change this functionality if you want to. There are two approaches to it, a good one and a bad one.

First approach is: Change you routes which maps users/:id to some other action of your controller

Second bad approach is to redirect to some other path within show action of users controller.

Hope this explanation is enough for your understanding

When you redirect_to an object in Rails (which is what you're doing), the framework has a lot of built-in functionality to take care of things for you

One of these in-built features is the ability to detect & redirect to the object itself


Objects

When you pass @user, you are passing a singular object, which Rails takes as meaning that you want to go to the show view (you want to view the object, right?)

You need to remember that Ruby / Rails is Object Orientated. You're calling them @instance variables, but really they are objects. Those objects contain a lot more data than your standard data - they contain information from the model & db

This means that if you build your @instance variable in the conventional Rails way (using Model data), you're going to get an object

--

Login

It seems to me that you're redirecting to the @user object. However, I would imagine this will be covnered by your authentication system, hence why you're receiving the login page


To fix this, you should try redirecting to specific paths, like this:

redirect_to users_path

Everyone has pointed out that controller interprets

redirect_to @user or redirect_to user_path(@user)

as

GET  /users/:id(.:format)    users#show

Question arises how Rails comes to know that out of so many routes it needs to go on to users#show

enter image description here

As you can see in above image that 3 paths are associated with
user_path & it requires id which you have passed using @user

So now controller ask which HTTP method GET/PATCH/PUT/DELETE

Since we have not specified any method it will automatically consider GET method & result is

users#show
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top