我有一个使用以下模型设置的项目。每个->
代表一个has_many
关系:
Users->Goals->Milestones
我的路线Milestones
看起来像这样:
user_goal_milestones GET /users/:user_id/goals/:goal_id/milestones(.:format) milestones#index
POST /users/:user_id/goals/:goal_id/milestones(.:format) milestones#create
new_user_goal_milestone GET /users/:user_id/goals/:goal_id/milestones/new(.:format) milestones#new
edit_user_goal_milestone GET /users/:user_id/goals/:goal_id/milestones/:id/edit(.:format) milestones#edit
user_goal_milestone GET /users/:user_id/goals/:goal_id/milestones/:id(.:format) milestones#show
PUT /users/:user_id/goals/:goal_id/milestones/:id(.:format) milestones#update
DELETE /users/:user_id/goals/:goal_id/milestones/:id(.:format) milestones#destroy
我发现自己在 Milestones 控制器中的许多“功能”中做了很多这样的事情:
def index do
@user = User.find(params[:user_id])
@goal = Goal.find(params[:goal_id])
end
def edit do
@user = User.find(params[:user_id])
@goal = Goal.find(params[:goal_id])
end
我怎样才能修改我的控制器,这样我就不必一直定义@user
了@goal
?我尝试将它们直接放在顶部,就在类定义块开始之后,但它不起作用。