【问题标题】:how to create route for history如何为历史创建路线
【发布时间】:2021-11-25 14:57:02
【问题描述】:

Index.html.slim

div class="container home"
  div class="row"
    div class="col"
      h1 = t '.main_header'
      h3 = link_to fa_icon('download'), admins_users_path(format: "csv")
      table class="table"
        thead
          tr
            td = t '.table.email_col'
            td = t '.table.last_visit_col'
            td = t '.table.view_info_col'
            td = t 'Edit'
            td = t 'History'

        - @users.each do |user|
          tr id="user-info-#{user.id}"
            td = user.email
            td = user.last_sign_in_at
            td = link_to fa_icon('eye'), admins_user_path(user.id)
            td = link_to fa_icon('edit'), edit_admins_user_path(user.id)
            td = link_to fa_icon('history'), admins_users_history_path(user.id)

用户控制器

module Admins
  class UsersController < ApplicationController
    rescue_from ActiveRecord::RecordNotFound, with: :render404
    layout 'admin'
    before_action :find_user, except: [:index]
    def index
      @users = User.all
      respond_to do |format|
        format.html
        format.csv do
          send_data UsersCsvGenerator.call(@users,
                                           fields: %w[email last_sign_in_at])
        end
      end
    end

    def edit
      @user = User.find(params[:id])
      render 'edit'
    end

    def update
      @user = User.find(params[:id])
      if @user.update(params.require(:user).permit(:email, :first_name,
                                                   :last_name, :password))
        redirect_to 'edit', notice: 'Successfully Updated'
      else
        @user.errors.full_messages
        render 'show'

      end
    end

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

    private

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

    def render404
      render file: "#{Rails.root}/public/404.html", layout: false, status: 404
    end
  end
end

路线

  namespace :admins do
    resources :users, only: %i[index show edit update history]
    get '/users/history', to: 'users#history'
    resources :calculators, only: %i[new create edit update]
  end

history.html.slim

- @user.versions.reverse.each do |version|
  div class="container"
  table class="table"
    thead
      tr
  th =  version.id
  th = 'Changes'
  th = version.created_at
  th = version.event
  br

你好 我有一个问题,我不知道如何为每个用户创建历史路线 当我在这里写的时候,在网络上我的链接看起来像 localhost:3000/admins/users/history.1 我需要 http://localhost:3000/admins/users/1/history

get '/users/history', 到:'users#history'

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

    :history 操作应该是member 上的嵌套get 路由,如下所示:

    #=> config/routes.rb
    
    Rails.application.routes.draw do
      namespace :admins do
        resources :users, only: %i[index show edit update history] do
          get :history, on: :member
        end
        resources :calculators, only: %i[new create edit update]
      end
    end
    

    结果:

    localhost:3000/admins/users/1/history
    

    参考:https://guides.rubyonrails.org/routing.html#adding-member-routes

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2013-04-08
      • 1970-01-01
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多