Reputation: 1
I developed a simple toy app which models a user and a micro-post using scaffolds. This is my user_controller
source "railstutorial.org"
class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
# GET /users
# GET /users.json
def index
@users = User.all
end
Method Show
# GET /users/1
# GET /users/1.json
def show
end
# GET /users/new
def new
@user = User.new
end
# GET /users/1/edit
def edit
end
# POST /users
# POST /users.json
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
Method update
# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.json
def destroy
@user.destroy
respond_to do |format|
format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def user_params
params.require(:user).permit(:name, :email)
end
end
Show method is used for rendering the user page with URL of the type "users/1".
However update method is to update our user's database, but after the update action is called it redirects us to the "users/1" URL.
In first case http request made is of type "GET" which routes us to "show" function/action, however in second case http request is of type "PATCH" which routes control to "update" function and this update function simply update the database, then why and how does it redirects us to "users/1". Does it call any rendering code somewhere ?
I am a beginner so please excuse me if question is a bit silly, but it would be a great help if someone could answer.
Upvotes: 0
Views: 46
Reputation: 167
@user is simply a user entry object who got an id in User Model, if you said redirect_to @user, it automatically detects the id of the user found in @user and redirects to /user/:id, rails is brave enough to understand that
Upvotes: 0
Reputation: 998
See at your code in update action after if @user.update(user_params)
You are calling redirect_to, it simply redirect you to new route which you provide it.
in this case its redirecting to show action as you are passing the object, you can provide any other route also.
read about redirect_to http://api.rubyonrails.org/classes/ActionController/Redirecting.html
Upvotes: 1