Search code examples
ruby-on-railsruby-on-rails-5friendly-id

Rails 5: FriendlyId TypeError (wrong argument type Symbol (expected Module:))


I am trying to add friendlyid to my application, after I've setup everything I get the following error:

TypeError wrong argument type Symbol expected Module

I inserted extend :FriendlyId and friendly_id :wine_name, use: :slugged into my model. I also tried to use slug instead of slugged but this didn't work.

I tried different paths but it doesn't seem to work

This is my 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
    [email protected] && [email protected]? && [email protected]_price.blank? && [email protected]_name.blank? && [email protected]? && [email protected]? && [email protected]? && [email protected]? && [email protected]_stock.blank? && [email protected]?
  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

Solution

  • I assume you use FriendlyId in your Wine model, the error is in there.

    It may look something like

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

    The error says that it was expecting a module but received a symbol, remove the : from FriendlyId

    class Wine < ApplicationRecord
      extend FriendlyId
      # ...
    end