【问题标题】:Rails 4 : How to disable the Edit, Destroy etc,Rails 4:如何禁用编辑、销毁等,
【发布时间】:2014-10-05 23:04:18
【问题描述】:

我可以在 Rails 中禁用“Edit”和“Destory”吗?例如,如果我想为每个人禁用“Edit”,我在 test_controller.rb 中做了什么显示?还是别的什么? 我是 Rails 新手,提前致谢!

  class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update,:destroy ]

  # GET /books
  # GET /books.json
  def index
    @books = Book.all
  end

  # GET /books/1
  # GET /books/1.json
  def show
  end

  # GET /books/new
  def new
    @book = Book.new
  end

  # GET /books/1/edit
def edit
end

  # POST /books
  # POST /books.json

  def create
    @book = Book.new(book_params)

    respond_to do |format|
      if @book.save
        format.html { redirect_to @book, notice: 'Book was successfully created.' }
        format.json { render :show, status: :created, location: @book }
      else
        format.html { render :new }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
   end


  # PATCH/PUT /books/1
  # PATCH/PUT /books/1.json

  def update
    respond_to do |format|
      if @book.update(book_params)
        format.html { redirect_to @book, notice: 'Book was successfully updated.' }
        format.json { render :show, status: :ok, location: @book }
      else
        format.html { render :edit }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /books/1
  # DELETE /books/1.json
  def destroy
    @book.destroy
    respond_to do |format|
      format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_book
      @book = Book.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def book_params
      params.require(:book).permit(:name, :author, :price)
    end
end

`Rails.application.routes.draw do
  resources :books

  root :to => "home#index"
  get 'home/index'
end`

【问题讨论】:

  • 请将您的test_controller.rb及相关路线发到routes.rb
  • 我有点困惑。在您的问题中,您提到了test_controller.rb,并且您发布了“books_controller.rb”的内容。您想让editdestroybooks_controller 操作对所有人都无法访问吗?
  • 是的,很抱歉我的改变。 book 代表test

标签: ruby-on-rails


【解决方案1】:

您可以限制 restful 路由以使 editdestroy 操作无法访问。

在你的 routes.rb 中,

resources :books, except: [:edit, :destroy]

见:http://guides.rubyonrails.org/routing.html#restricting-the-routes-created


编辑

如果您想保持 RESTful 路由(这样您就不必修改视图中的代码),您可以在控制器中使用 before_action 来重定向用户。

before_action :redirect_user, only: [:edit,:destroy]

def redirect_user
  redirect_to root_path
end

当您想根据某些条件限制对某些操作的访问时,通常会使用此方法。

例如,如果您只想让管理员编辑和删除书籍,您可以在redirect_user 中设置条件来检查当前用户是否为管理员并重定向非管理员用户。

【讨论】:

  • 谢谢,它有效! :) 但我必须手动删除/view/index.html.erbshow.html.erb 中包含"Edit" 的一些代码:(
  • 查看编辑。您想保留代码但不希望任何人访问它是不寻常的。
【解决方案2】:

您应该查看康康康宝石。 https://github.com/CanCanCommunity/cancancan

它是 Ruby on Rails 的授权库,它限制了给定用户可以访问的资源。所以你可以创建一个管理员类,并且只允许管理员编辑和销毁。它使用起来非常简单,并且与设计配合得很好。

【讨论】:

  • 谢谢您的回答,我要去看看康康康舞宝石。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多