我怎么能说从我的r_holidays_controller方法?
下面是我的脚本是尝试,但它不会工作。
$(函数(){
$('#toright)。点击(函数(){
VAR SEL =的document.getElementById(左);
VAR的len = sel.options.length;
变种w_id = this.getAttribute('w_id');
对于(VAR I = 0; I< LEN;我++)
{
如果(sel.options [I] .selected)
{
$阿贾克斯(/ r_holidays / w_destroy,{holiday_id:sel.options [I] .value的,GROUP_ID:w_id})。完成(功能(数据){});
}
}
history.go(0);
})
});
我认为,必须有一个错误在我的 $。阿贾克斯(...)
。
国有企业的人有一个想法,得到这个工作? 当检查我的pathes,我意识到,我没有列出...
r_holidays_path GET /r_holidays(.:format)r_holidays#指数
POST /r_holidays(.:format)r_holidays#创建
new_r_holiday_path GET /r_holidays/new(.:format)r_holidays#新
edit_r_holiday_path GET /r_holidays/:id/edit(.:format)r_holidays#编辑
r_holiday_path GET /r_holidays/:id(.:format)r_holidays#秀
PATCH /r_holidays/:id(.:format)r_holidays#更新
PUT /r_holidays/:id(.:format)r_holidays#更新
DELETE /r_holidays/:id(.:format)r_holidays#销毁
编辑:
下面是我的控制器code:
类RHolidaysController<的ApplicationController
before_action:set_r_holiday,只有:[:显示,编辑,:更新:灭]
高清指数
@r_holidays = RHoliday.all
结束
高清新
@r_holiday = RHoliday.new
结束
高清编辑
结束
DEF创建
渲染JSON:RHoliday.find_or_create(holiday_id:PARAMS [:holiday_id] .to_s,GROUP_ID:PARAMS [:GROUP_ID] .to_s)
结束
高清w_create
@r_holiday = RHoliday.new(r_holiday_params)
respond_to代码做|格式|
如果@ r_holiday.save
的format.html {redirect_to时@r_holiday,通知:RHoliday已成功创建 }
format.json {渲染动作:'秀',状态:创建,地点:@r_holiday}
其他
的format.html {渲染的动作:新的'}
format.json {渲染JSON:@ r_holiday.errors,状态:unprocessable_entity}
结束
结束
结束
DEF更新
respond_to代码做|格式|
如果@ r_holiday.update(holiday_params)
的format.html {redirect_to时@r_holiday,通知:RHoliday已成功更新 }
format.json {头:} NO_CONTENT
其他
的format.html {渲染动作:'编辑'}
format.json {渲染JSON:@ r_holiday.errors,状态:unprocessable_entity}
结束
结束
结束
DEF破坏
@ r_holiday.destroy
respond_to代码做|格式|
的format.html {redirect_to时holidays_url}
format.json {头:} NO_CONTENT
结束
结束
高清w_destroy
渲染JSON:RHoliday.where(holiday_id:PARAMS [:holiday_id] .to_s,GROUP_ID:PARAMS [:GROUP_ID] .to_s).destroy
结束
私人
高清set_holiday
@r_holiday = RHoliday.find(PARAMS [:ID])
结束
高清holiday_params
params.require(:r_holiday).permit(:holiday_id,:GROUP_ID)
结束
结束
和我routes.rb中的相关行(S)就是资源:r_holidays
$。阿贾克斯({
网址:/ r_holidays / w_destroy
数据类型:HTML,
数据: {
holiday_id:sel.options [I]。价值,
GROUP_ID:w_id
},
})
.done(函数(){
的console.log(成功);
});
您需要传递要删除记录的ID。然后它会调用方法,从URL,你可以找到一个方法来记录你所拥有的传递从$就数据。
URL:/ r_holidays /:ID / w_destroy我认为你需要在这里通过一个标识。
VAR my_url =/ rholidays /+<%= @ object.id%> +/ w_destroy;
通过my_url的网址:my_url,在$阿贾克斯
你的方法应该包含的内容。
数据= PARAMS [:holiday_id]
组= PARAMS [:GROUP_ID]
节日= Holiday.find(数据)
holiday.destroy
这样,您就可以摧毁的方法。或轨道提供了一个最好的AJAX方法(RJS)。你为什么不试试呢?
How can I call a Method from my r_holidays_controller?
Here is my try in scripting it, but it won't work.
$(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);
})
});
I think, there must be a mistake in my $.ajax( ... )
.
Soes anyone have an Idea to get this working? When checking my pathes, I realize, that my aren't listed...
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
EDIT:
Here's my controller code:
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
And the relevant line(s) in my routes.rb is just resources :r_holidays
$.ajax({
url: "/r_holidays/w_destroy",
dataType: 'html',
data: {
holiday_id: sel.options[i].value,
group_id: w_id
},
})
.done(function() {
console.log("success");
});
You need to pass ID of record that you want to delete. and then it will call method from url and you can find that record in a method what you have pass with data from $.ajax.
url: "/r_holidays/:id/w_destroy" I think you need to pass a Id here.
var my_url = "/rholidays/" + <%= @object.id %> + "/w_destroy";
pass my_url as url: my_url, in $.ajax
your method should contain.
data = params[:holiday_id]
group = params[:group_id]
holiday = Holiday.find(data)
holiday.destroy
This way you can destroy method. or rails provides a best ajax method (Rjs). why dont you try that?
相关推荐
最新文章