Ruby on rails Rails http rest体系结构

Ruby on rails Rails http rest体系结构,ruby-on-rails,rest,http,Ruby On Rails,Rest,Http,我开发了一个简单的玩具应用程序,它使用脚手架模拟用户和一个微型帖子。这是我的用户控制器 来源“railstutorial.org” 方法更新 # 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: 'Us

我开发了一个简单的玩具应用程序,它使用脚手架模拟用户和一个微型帖子。这是我的用户控制器

来源“railstutorial.org”


方法更新

  # 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

Show方法用于呈现URL类型为“users/1”的用户页面

然而,更新方法是更新用户的数据库,但在调用更新操作后,它会将我们重定向到“users/1”URL

在第一种情况下,http请求的类型为“GET”,它将我们路由到“show”函数/操作,而在第二种情况下,http请求的类型为“PATCH”,它将控制路由到“update”函数,而此更新函数只是更新数据库,那么它为什么以及如何将我们重定向到“users/1”。它是否在某处调用任何渲染代码


我是一个初学者,请原谅我的问题有点傻,但如果有人能回答,这将是一个很大的帮助。

请在if@user.update(user\u params)之后查看更新操作中的代码

您正在调用redirect_to,它只是将您重定向到您提供的新路由。
在这种情况下,当您传递对象时,它会重定向以显示操作,您也可以提供任何其他路由。


阅读关于重定向到

@user只是一个在用户模型中获得id的用户条目对象,如果您说重定向到@user,它会自动检测在@user中找到的用户的id并重定向到/user/:id,rails很勇敢地理解这一点

哦,谢谢。我不知道我怎么会错过它。非常感谢。
  # 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
  # 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