没有路由匹配{:action =>"编辑",:controller =>"事件"}缺少必需的密钥:[:id]

时间:2014-04-30 20:11:51

标签: ruby-on-rails events controller routes action

我正在尝试添加"编辑事件"链接,我收到了以下错误。

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

如果有人能够弄清楚这个错误,我会非常感激。 :)

1 个答案:

答案 0 :(得分:10)

您需要传递要编辑的id event。目前你没有传递它导致错误

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

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

的迭代中
= link_to "Edit", edit_event_path(event)

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