Ruby on rails FriendlyId改为重定向到篮子或更正链接

Ruby on rails FriendlyId改为重定向到篮子或更正链接,ruby-on-rails,ruby,friendly-id,Ruby On Rails,Ruby,Friendly Id,我刚刚安装了FriendlyID来更改我网站的URL。 我已经为类别设置了此选项,尽管链接会发生变化,当我单击它们时,url会变为basket/(正确的产品) 这真的是一个bug,我已经阅读了下面的代码,删除了所有的篮子引用,但它仍然存在。 有人能给我指出正确的方向吗 Category.rb class Category < ActiveRecord::Base extend FriendlyId friendly_id :title, use: [:slugged, :histo

我刚刚安装了FriendlyID来更改我网站的URL。 我已经为类别设置了此选项,尽管链接会发生变化,当我单击它们时,url会变为
basket/(正确的产品)

这真的是一个bug,我已经阅读了下面的代码,删除了所有的篮子引用,但它仍然存在。 有人能给我指出正确的方向吗

Category.rb

class Category < ActiveRecord::Base
  extend FriendlyId
  friendly_id :title, use: [:slugged, :history]
  has_many :products
end
My categories\u controller.rb

class CategoriesController < ApplicationController
  before_action :set_category, only: [:show, :edit, :update, :destroy]
   before_action :set_basket, only: [:show, :destroy]
  rescue_from ActiveRecord::RecordNotFound, with: :invalid_basket

  # GET /categories
  # GET /categories.json
  def index
    @categories = Category.all
    @top_ups = @categories [8]
    @log_stores = @categories [6]
    @smokeless_coal = @categories [5]
    @log_packages = @categories [4]
    @seasoned_logs = @categories [3]
    @kiln_dried_hardwood_logs = @categories [1]
    @logs_for_pizza_ovens = @categories [2]
    @logs_for_firepits = @categories [9]
  end

  # GET /categories/1
  # GET /categories/1.json
  def show
    @products = Product.where(category_id: params[:id])
    if request.path != category_path(@category)
      redirect_to @category, status: :moved_permanently
    end
  end

  # GET /categories/new
  def new
    @category = Category.new
  end

  # GET /categories/1/edit
  def edit
  end

  # POST /categories
  # POST /categories.json
  def create
    @category = Category.new(category_params)

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

  # PATCH/PUT /categories/1
  # PATCH/PUT /categories/1.json
  def update
    respond_to do |format|
      if @category.update(category_params)
        format.html { redirect_to @category, notice: 'Category was successfully updated.' }
        format.json { render :show, status: :ok, location: @category }
      else
        format.html { render :edit }
        format.json { render json: @category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /categories/1
  # DELETE /categories/1.json
  def destroy
    @category.destroy
    respond_to do |format|
      format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def category_params
      params.require(:category).permit(:title, :description)
    end
     def set_basket
        @basket = Basket.find(params[:id])
    end

    def basket_params
        params[:basket]
    end

    def invalid_basket
        logger_error = 'Your trying to access an invalid basket'
        redirect_to basket_url, notice: 'Invalid basket'
    end
end
class BasketsController < ApplicationController

    before_action :set_basket, only: [:show, :destroy]
    rescue_from ActiveRecord::RecordNotFound, with: :invalid_basket

    def new
        @basket = Basket.new
    end


    def show
         @basket = Basket.find(params[:id])
    end
    def index
        @basket = Basket.find(params[:id])
    end


    def destroy
        @basket.destroy if @basket.id == session[:basket_id]
        session[:basket_id] = nil
        redirect_to home_url, notice: 'Your basket is empty'
    end

    private

    def set_basket
        @basket = Basket.find(params[:id])
    end

    def basket_params
        params[:basket]
    end

    def invalid_basket
        logger_error = 'Your trying to access an invalid basket'
        redirect_to basket_url, notice: 'Invalid basket'
    end
end
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  before_action :set_basket, only: [:show, :destroy]
  protect_from_forgery with: :exception



  def set_basket
    @basket = Basket.find(params[:id])
  end
  private
  def basket_params
      params[:basket]
  end

  def invalid_basket
      logger_error = 'Your trying to access an invalid basket'
      redirect_to basket_url, notice: 'Invalid basket'
  end
end
Routes.rb

Rails.application.routes.draw do
  mount Ckeditor::Engine => '/ckeditor'

  resources :vans
  devise_for :users
  # Change get method for user signout 
  devise_scope :user do
    get '/users/sign_out' => 'devise/sessions#destroy'
  end


  root 'pages#index'

  resources :categories 
  resources :products
  resources :drivers
  resources :product_items
  resources :baskets
  resources :orders
  resources :customers

  match '/areascovered', to: 'pages#areascovered', via: :get

  match '/deliveryschedule', to: 'pages#deliveryschedule', via: :get

  match '/news', to: 'pages#news', via: :get

  match '/faqs', to: 'pages#faqs', via: :get

  match '/about', to: 'pages#about', via: :get

  match '/contact', to: 'pages#contact', via: :get

  get "baskets" => "baskets#show", :as => "current_basket"
end

您需要在控制器中使用友好的find方法。例如,代替

def set_category
  @category = Category.find(params[:id])
end
你应使用:

def set_category
  @category = Category.friendly.find(params[:id])
end
此外,还要注意执行两次
find
操作的情况。例如,在您的
baskets控制器#show
操作中,您正在调用
set\u basket
(当前调用
basket.find(params[:id])
,您应该更改它以执行
basket.friendly.find(params[:id])
在_action之前通过,但随后在#show操作的主体中再次设置相同的实例变量。您的BasketsController#show操作应为:

def show
end

感谢您的帮助。感谢您的帮助。我现在遇到了产品未显示到类别的问题。参数[:id]现在不再匹配,因为它等于新的url路径。很抱歉,如果这是基本的,我是ruby新手。我不确定您所说的“参数[:id]现在不再匹配”是什么意思您遇到的错误是什么?我正在尝试显示每个类别中的产品。我通过以下操作完成了此操作,但是,现在产品将不会显示。
def show@products=Product.where(category\u id:params[:id])if request.path!=category\u path(@category\u id)重定向到@category,status::moved\u permanually end
在执行操作之前,您已经在
中设置了
@category
,所以只需执行
@products=Product。其中(category:@category)
。一般来说,在已经有对象的地方,最好传递该对象并让Rails插值,而不是传递id或slug。
def set_category
  @category = Category.find(params[:id])
end
def set_category
  @category = Category.friendly.find(params[:id])
end
def show
end