Ruby on rails - 没有路由匹配 {:action=> "edit" , :controller=> "events" } 缺少必需的键:[:id]



我正在开发Rails应用程序,在尝试添加"编辑事件"链接后,我收到了以下错误。

No route matches {:action=>"edit", :controller=>"events"} missing required keys: [:id]

下面是我的index.html.haml

.event_con
  %h1 Things you've done
  - @events_own.each do |event|
    .event_title
      = event.activity
    .event_rating
      %strong Rating : 
      = event.rating
      &#47 10
    .event_when
      %strong Started : 
      = event.time.to_s(:short)
    .event_end
      %strong Ended : 
      = event.timetwo.to_s(:short)
    .event_notes
      %strong Notes :
      = event.notes
    = link_to "Edit", edit_event_path

这是我的事件控制器:

class EventsController < ApplicationController
  before_action :set_event, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  # GET /events
  # GET /events.json
  def index
    @events = Event.all.order("created_at DESC")
    @events_own = Event.where(:userid => current_user.id).order("created_at DESC")
    @event = Event.new
  end
  # GET /events/1
  # GET /events/1.json
  def show
  end
  # GET /events/new
  def new
    @event = Event.new
    config.time_zone = 'London'
  end
  # GET /events/1/edit
  def edit
  end
  # POST /events
  # POST /events.json
  def create
    @event = Event.new(event_params)
    @event.userid = current_user.id
    config.time_zone = 'London'

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render action: 'show', status: :created, location: @event }
      else
        format.html { render action: 'new' }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /events/1
  # PATCH/PUT /events/1.json
  def update
    respond_to do |format|
      if @event.update(event_params)
        format.html { redirect_to @event, notice: 'Event was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /events/1
  # DELETE /events/1.json
  def destroy
    @event.destroy
    respond_to do |format|
      format.html { redirect_to events_url }
      format.json { head :no_content }
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_event
      @event = Event.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def event_params
      params.require(:event).permit(:activity, :time, :rating, :notes, :userid, :id, :timetwo)
    end
end

如果有人能找出这个错误,我将非常感激。:(

您需要传递想要编辑的eventid。目前你没有通过它,这导致错误

No route matches {:action=>"edit", :controller=>"events"} missing required keys: [:id]

要解决此问题,请使用以下更新的链接,该链接应出现在@events_own 的迭代中

= link_to "Edit", edit_event_path(event)

这将为每个event正确创建edit链接。

最新更新