我有一些嵌套路由设置,如下所示:-
resources :events, only: :index
resources :organisations do
resources :events, except: [:index]
end
允许我有以下路线:-
/events
/organisations/1/events/1
上述两项都很好,但是,我可以更改url中的事件id,它会给我一个不属于该组织的事件,但该组织id将保持不变:-
/organisations/1/events/2
i、 e.这将向我展示活动2,但该活动的组织机构现在是组织机构1。
在我的eventscontroller中,我只有通常生成的普通路径:-
class EventsController < ApplicationController
before_action :set_event, only: %i[show edit update destroy]
before_action :set_organisation, except: %i[index]
# GET /events/1
def show; end
# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find(params[:id])
end
def set_organisation
@organisation = Organisation.find(params[:organisation_id])
end
我的问题:-rails应该知道如何处理这个问题,并且我是否在这里设置了错误的内容,还是只需要在我的 show
方法,当前事件是否属于当前组织(如果不是,则重定向到某个位置)?
1条答案
按热度按时间blpfk2vs1#
您需要手动处理它。
在SETU事件中
Event.find(params[:id])
直接查找记录而不进行任何组织操作检查。处理它的一种方法是使用作用域。如果您传递的事件id不属于组织,您将收到recordnotfound。
此外,您还需要更改回调的顺序:应在set_事件之前调用set_组织。