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
/ 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. :)
You need to pass the
id
ofevent
which you would like to be edited. Currently you are not passing it which is causing the errorTo resolve this issue, use the below updated link and it should be present within the iteration on
@events_own
This will create
edit
links for eachevent
correctly.