如何在Rails中向自定义控制器方法添加路由?
我有以下方法,想通过AJAX调用它们。
def w_destroy
render json: RHoliday.where(holiday_id: params[:holiday_id].to_s, group_id: params[:group_id].to_s).destroy
end
def w_create
@r_holiday = RHoliday.new(r_holiday_params)
respond_to do |format|
if @r_holiday.save
format.html { redirect_to @r_holiday, notice: 'RHoliday was successfully created.' }
format.json { render action: 'show', status: :created, location: @r_holiday }
else
format.html { render action: 'new' } format.json { render json: @r_holiday.errors, status: :unprocessable_entity }
end
end
end
问题是:
我该怎么做呢?我想我需要编辑我的routes.rb,但我不知道该添加什么。我刚刚使用了resources :r_holidays
,但这只是创建了默认路由。
答案 0 :(得分:2)
您可以通过多种不同的方式add extra routes out of resources scope:
#config/routes.rb
resources :r_holidays do
post :w_create
delete :w_delete
end
具体来说,resources
电话基本上会给你Rails'标准7 RESTful路由 - 您可以使用代码块根据需要定义额外路由。您应该read the documentation获得更多想法