2014-03-31 33 views
1

如何从我的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

+0

我们可以看到控制器代码和routes.rb的相关部分吗? – akatakritos

+0

您是否在路线文件中添加了w_destroy动作? –

+0

我没有。我必须在哪里添加它? 我刚刚添加了资源:r_holidays到我的routes.rb。 我还需要在那里添加什么? – user3383458

回答

1
$.ajax({ 
    url: "/r_holidays/w_destroy", 
    dataType: 'html', 
    data: { 
     holiday_id: sel.options[i].value, 
     group_id: w_id 
    }, 
    }) 
    .done(function() { 
    console.log("success"); 
    }); 

您需要传递您想要删除的记录的ID。然后它会从url调用方法,并且您可以在方法中找到您使用$ .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 

你的方法应该包含。

data = params[:holiday_id] 
group = params[:group_id] 
holiday = Holiday.find(data) 
holiday.destroy 

这样你可以销毁方法。或者rails提供了最好的ajax方法(Rjs)。你为什么不试试?

2

我认为你使用$.get语法与$.ajax

尝试用$.get代替$.ajax

+0

谢谢!我试过了,但它不起作用。 我试图做到这一点,创建/销毁表中的行。但它不起作用。 – user3383458

+1

啊。您的操作是否可以通过GET路径访问,或者只有POST/PUT/DELETE? – Chowlett

+0

$ .get将跟随获取路由,通过哪条路由到'r_holidays#show',您需要使用DELETE'方法调用 – engineersmnky

1

你的代码似乎是正确的,但有一件事你在这里失踪。

你可以注意到你的控制器接收到GET,POST,PUT,DELETE等请求。这意味着你的.ajax方法应该指定你发送的请求的类型。

如果你想调用破坏你r_holidays控制器的动作,你应该尝试这样的水木清华:

$.ajax("/r_holidays", type: 'DELETE', {holiday_id: sel.options[i].value, group_id: w_id}).done(function(data){}); 

希望这有助于!

+0

当我添加此行时,由于类型:'DELETE',我得到语法错误。这是正常的吗? – user3383458

0

Chowlett的答案对于GET和POST是正确的 - 用'get'/'post'替换'ajax'。或者通过传递设置对象来使用完整的'ajax'语法:https://api.jquery.com/jQuery.ajax/。请注意,浏览器通过AJAX支持PUT和DELETE方法的历史可疑,正如jQuery.ajax API文档中针对'type'设置所述。

如果你的后端通过POST支持HTTP方法仿真,就像Rails会,我已经写了jQuery插件与便利$。放,$ .patch和$ .delete方法:

https://github.com/adjohnson916/jquery-methodOverride

看看吧!

相关问题