2014-03-31 32 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

看看吧!

相關問題