【问题标题】:Rails 5: FriendlyId TypeError (wrong argument type Symbol (expected Module:))Rails 5: FriendlyId TypeError (错误的参数类型 Symbol (expected Module:))
【发布时间】:2018-11-02 16:29:10
【问题描述】:

我正在尝试将friendlyid 添加到我的应用程序中,在我设置完所有内容后,我收到以下错误:

TypeError 错误参数类型符号预期模块

我将extend :FriendlyIdfriendly_id :wine_name, use: :slugged 插入到我的模型中。我也尝试使用 slug 而不是 slugged,但这没有用。

我尝试了不同的路径,但它似乎不起作用

这是我的 wines_controller

class WinesController < ApplicationController
  before_action :set_wine, except: [:index, :new, :create]
  before_action :authenticate_user!, except: [:show]
  before_action :is_authorised, only: [:details, :pricing, :description, :photo_upload, :more_details, :sets, :location, :update]

  def index
    @wines = current_user.wines
  end

  def new
    @wine = current_user.wines.build
    redirect_to root_path unless current_user.admin?
  end

  def create
    @wine = current_user.wines.build(wine_params)
    if @wine.save
      redirect_to details_wine_path(@wine), notice: "Saved..."
    else
      flash[:alert] = "Something went wrong..."
      render :new
    end
  end

  def show
    @photos = @wine.photos
    @guest_reviews = @wine.guest_reviews
  end

  def details
  end

  def pricing
  end

  def description
  end

  def photo_upload
    @photos = @wine.photos
  end

  def more_details
  end

  def sets
  end

  def location
  end

  def update

    new_params = wine_params
    new_params = wine_params.merge(active: true) if is_ready_wine

    if @wine.update(wine_params)
      flash[:notice] = "Saved..."
    else
      flash[:alert] = "Something went wrong..."
    end
    redirect_back(fallback_location: request.referer)
  end

  def preload
    today = Date.today
    # Reservations greater than today (upcoming reservations)
    reservations = @wine.reservations.where("start_date >= ?", today)

    render json: reservations
  end


  private
  def set_wine
    @wine = Wine.friendly.find(params[:id])
  end
  # Authorize user (current_user == ID 1)
  def is_authorised
    redirect_to root_path, alert: "You don't have permission" unless current_user.id == @wine.user_id
  end

  def is_ready_wine
    !@wine.active && !@wine.price.blank? && !@wine.base_price.blank? && !@wine.wine_name.blank? && !@wine.summary.blank? && !@wine.photos.blank? && !@wine.alcohol.blank? && !@wine.filling.blank? && !@wine.in_stock.blank? && !@wine.address.blank?
  end

  def wine_params
    params.require(:wine).permit(:wine_type, :wine_color, :wine_art, :alcohol, :wine_name, :summary, :address, :is_1, :is_3, :is_6, :is_12, :filling, :base_price, :price, :in_stock, :year, :active)
  end
end

【问题讨论】:

  • 您正在使用符号参数 (extend :FriendlyId) 调用 extend,但应使用模块调用 extend,因为错误状态。也许你的意思是说extend FriendlyId

标签: ruby-on-rails ruby-on-rails-5 friendly-id


【解决方案1】:

我假设您在 Wine 模型中使用 FriendlyId,错误就在那里。

它可能看起来像

class Wine < ApplicationRecord
  extend :FriendlyId
  # ...
end

错误说它期待一个模块但收到一个符号,从FriendlyId中删除:

class Wine < ApplicationRecord
  extend FriendlyId
  # ...
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 2021-07-19
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多