如何從我的r_holidays_controller調用方法?如何從rails中的控制器調用方法?
這是我的嘗試在腳本中,但它不會工作。
$(function() {
$('#toright').click(function() {
var sel = document.getElementById("left");
var len = sel.options.length;
var w_id = this.getAttribute('w_id');
for (var i = 0; i < len; i++)
{
if(sel.options[i].selected)
{
$.ajax("/r_holidays/w_destroy", {holiday_id: sel.options[i].value, group_id: w_id}).done(function(data){});
}
}
history.go(0);
})
});
我想,在我的$.ajax(...)
中一定有一個錯誤。
Soes any people have a idea to get this working? 當檢查我的pathes,我意識到,我沒有列出...
r_holidays_path GET /r_holidays(.:format) r_holidays#index
POST /r_holidays(.:format) r_holidays#create
new_r_holiday_path GET /r_holidays/new(.:format) r_holidays#new
edit_r_holiday_path GET /r_holidays/:id/edit(.:format) r_holidays#edit
r_holiday_path GET /r_holidays/:id(.:format) r_holidays#show
PATCH /r_holidays/:id(.:format) r_holidays#update
PUT /r_holidays/:id(.:format) r_holidays#update
DELETE /r_holidays/:id(.:format) r_holidays#destroy
編輯:
這裏是我的控制器代碼:
class RHolidaysController < ApplicationController
before_action :set_r_holiday, only: [:show, :edit, :update, :destroy]
def index
@r_holidays = RHoliday.all
end
def new
@r_holiday = RHoliday.new
end
def edit
end
def create
render json: RHoliday.find_or_create(holiday_id: params[:holiday_id].to_s, group_id: params[:group_id].to_s)
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
def update
respond_to do |format|
if @r_holiday.update(holiday_params)
format.html { redirect_to @r_holiday, notice: 'RHoliday was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @r_holiday.errors, status: :unprocessable_entity }
end
end
end
def destroy
@r_holiday.destroy
respond_to do |format|
format.html { redirect_to holidays_url }
format.json { head :no_content }
end
end
def w_destroy
render json: RHoliday.where(holiday_id: params[:holiday_id].to_s, group_id: params[:group_id].to_s).destroy
end
private
def set_holiday
@r_holiday = RHoliday.find(params[:id])
end
def holiday_params
params.require(:r_holiday).permit(:holiday_id, :group_id)
end
end
及相關線(S)在我的routes.rb只是resources :r_holidays
我們可以看到控制器代碼和routes.rb的相關部分嗎? – akatakritos
您是否在路線文件中添加了w_destroy動作? –
我沒有。我必須在哪裏添加它? 我剛剛添加了資源:r_holidays到我的routes.rb。 我還需要在那裏添加什麼? – user3383458