Git Product home page Git Product logo

token-sample's Introduction

README

What is this repository

Rails 7.1にて追加されたActiveRecord::Base.generates_token_forを試してみるためのサンプルアプリケーション。

リリースノート:https://railsguides.jp/7_1_release_notes.html

How ActiveRecord::Base.generates_token_for is used

1. Taskモデルにて、5秒でexpireされる:upadte_taskトークンを生成する定義を追加

# app/models/task.rb
class Task < ApplicationRecord
  generates_token_for :update_task, expires_in: 5.seconds
end

2. Task編集ページへのアクセス時にtokenを発行し、hidden_fieldに値をセット。

<!-- app/views/tasks/edit.html.erb -->
<%= form_with(model: task, local: true) do |form| %>
  ...
  <% if @task.persisted? %>
    <%= form.hidden_field :update_task_token, value: @task.generate_token_for(:update_task) %>
  <% end %>
  ...
<% end %>

3. PUTリクエスト時のパラメーターにtokenを含めて送信

4. Updateアクションで受け取ったtokenからTaskを取得し、取得できない場合はメッセージを含めて編集ページを再renderする

# app/controllers/tasks_controller.rb
def update
  @task = Task.find_by_token_for(:update_task, params[:task][:update_task_token])

  if @task.nil?
    set_task
    @task.errors.add(:base, STUPID_TIMEUP_MESSAGE)
    render :edit, status: :unprocessable_entity
  else
    respond_to do |format|
      if @task.update(task_params)
        format.html { redirect_to task_url(@task), notice: "Task was successfully updated." }
        format.json { render :show, status: :ok, location: @task }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @task.errors, status: :unprocessable_entity }
      end
    end
  end
end

これらの実装により、編集ページへアクセスしてから5秒以内にSubmitしなければ更新できないTask管理アプリケーションが完成した。

Demo

5秒以内にSubmitできなかったとき

Image from Gyazo

5秒以内にSubmitできたとき

Image from Gyazo

token-sample's People

Contributors

tarakish avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.