"undefined method `slug'" 尝试使用 Friendly_id 保存嵌套路线时 5

"undefined method `slug'" when trying to save nested route using Friendly_id 5

我的应用程序允许用户创建活动,人们可以回复该活动。该应用程序在添加 Friendly_id gem 之前运行良好,就创建新事件而言它运行良好,但 RSVP 不起作用,因为当您单击提交时会出现错误。请查看我的代码,看看是否有任何遗漏。提前谢谢你。

事件模型

 class Event < ApplicationRecord
 extend FriendlyId
 friendly_id :eventname, use: [:slugged, :finders]
   belongs_to :user


def should_generate_new_friendly_id?
    eventname_changed?
end
   has_attached_file :image, styles: { medium: "300x300>", thumb:   "100x100>" }, default_url: "/images/:style/placeholder.png"
   validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
   validates :eventname, presence: true
   has_many :rsvps, dependent: :destroy
end'

回复模型

class Rsvp < ApplicationRecord
    extend FriendlyId
    friendly_id :eventname, use: [:slugged, :finders]
    belongs_to :event
end

事件控制器

class EventsController < ApplicationController
before_filter :authenticate_user!, except: [:show]

def index
    @events = current_user.events.all
end

def show
    @event = Event.friendly.find(params[:id])
end

def new
    @event = current_user.events.build
end

def create
    @event = current_user.events.build(event_params)
    @event.user = current_user
    respond_to do |format|
        if @event.save
            format.html { redirect_to @event, notice: "Successfully created" }
        else
            format.html { render "new" }
        end
    end
end

def edit
    @event = Event.friendly.find(params[:id])
end

def update
    @event = Event.friendly.find(params[:id])
    if @event.update(event_params)
        redirect_to @event
    else
        render 'edit'
    end
end

def destroy
    @event = Event.friendly.find(params[:id])
    @event.destroy

    redirect_to events_path
end

private

 def event_params
  params.require(:event).permit(:eventname, :date, :time, :venue,   :description, :image)
end

end

Rsvp 控制器

class RsvpsController < ApplicationController

def index
    event = Event.friendly.find(params[:event_id])
    @rsvps = event.rsvps
end

def new
    event = Event.friendly.find(params[:event_id])
    @rsvp = event.rsvps.friendly.build
        respond_to do |format|
            format.html
        end
end

def create
    event = Event.friendly.find(params[:event_id])
    @rsvp = event.rsvps.build(rsvp_params)
    respond_to do |format|
        if @rsvp.save
        format.html { redirect_to "/thanks" }
        format.js
        else
        format.html { render :new }
        format.js
        end
    end
end

def thanks
    render params[:page]
end

 private
    def rsvp_params
    params.require(:rsvp).permit(:status, :name, :message)
    end

 end

路线

  resources :events do
   resources :rsvps
  end

显示事件:

http://127.0.0.1:3000/events/birthday

为活动创建 rsvp:

 http://127.0.0.1:3000/events/3/rsvps/new

提交后错误显示:

http://127.0.0.1:3000/events/birthday/rsvps

Screenshot of Error I get

我通过更新以下内容设法解决了这个问题:

class Rsvp < ApplicationRecord
   belongs_to :event
end

改变了这个:

new_event_rsvp_path(event_id: @event.id)

对此:

new_event_rsvp_path(@event)

创建新的 RSVP 现在以 Friendly_id 方式显示:

 http://127.0.0.1:3000/events/birthday/rsvps/new