没有符合{:action =>“edit”,:controller =>“events”的路由}缺少必需的键:[:id] [英] No route matches {:action=>"edit", :controller=>"events"} missing required keys: [:id]
问题描述
我在使用Rails应用程式,尝试加入「编辑活动」连结后,发生以下错误。
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]
下面是我的index.html.haml
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. :)
推荐答案
您需要传递 id
$ c> event 。
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]
要解决此问题,请使用下面更新的链接,它应该在 @events_own
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.
这篇关于没有符合{:action =>“edit”,:controller =>“events”的路由}缺少必需的键:[:id]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!