No route matches {:action=>“edit”, :controller=>“e

2020-07-30 02:03发布

i'm working on a Rails app and after trying to add a "Edit Event" link, I've been getting the following error.

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

Down below is my 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

And here is my Events controller :

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

If anyone would be able to figure out this bug I would really appreciate it. :)

1条回答
倾城 Initia
2楼-- · 2020-07-30 02:44

You need to pass the id of event which you would like to be edited. Currently you are not passing it which is causing the error

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

To resolve this issue, use the below updated link and it should be present within the iteration on @events_own

= link_to "Edit", edit_event_path(event)

This will create edit links for each event correctly.

查看更多
登录 后发表回答