gpt4 book ai didi

jquery - 如何从 rails 中的 Controller 调用方法?

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:05 25 4
gpt4 key购买 nike

如何从我的 r_holidays_controller 调用方法?

这是我编写脚本的尝试,但它行不通。

$(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( ... ) 一定有错误。

所以有人有想法让这个工作吗?检查我的路径时,我意识到我的路径没有列出...

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

编辑:

这是我的 Controller 代码:

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

我的 routes.rb 中的相关行只是 resources :r_holidays

最佳答案

我认为您正在使用 $.get $.ajax 的语法.

尝试用 $.get 替换你的 $.ajax

关于jquery - 如何从 rails 中的 Controller 调用方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22763978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com