2016-04-27 157 views
0

我有一些Ajax函數的問題。我從服務器收到錯誤500rails ajax請求返回未找到但控制器功能存在

Ajax的功能如下:

$.ajax({ 
    type: "POST", 
    url: "<%= url_for(:controller => "movies", :action => "test") %>", 
    data: {inputtag: tag } 
    }) 

在我的電影控制器,我有這個功能

# Fügt dem Video einen Tag hinzu 
def test 
    @tag = Tag.new 
    if request.post? 
     @tag.update_attributes(params[:inputtag]) 
     if @tag.save 
     redirect_to :back 
     else 
     redirect_to :back 
     end 
    end 
    end 

所以,我不知道爲什麼我得到這個錯誤:

http://lvh.me/movies/test 500 (Internal Server Error) 
+0

你能發佈你得到的實際Ruby錯誤嗎? –

+0

Url退出,這就是爲什麼你面臨500錯誤,現在粘貼我正面臨的確切錯誤 –

+0

不應該插入字符串'<%='以'%>'結尾,而不是'=>'? – CBusBus

回答

0

您缺少routes.rb文件中的條目

resources :movies do 
    collection do 
    get 'test' 
    end 
end 
0

500狀態不是關於路由。檢查你的控制器動作,具體是這個。

@tag.update_attributes(params[:inputtag])

您正在嘗試更新不存在的記錄,並且您沒有正確使用Rails的強參數。所以試試這個。

def test 
    @tag = Tag.create tag_params 
    redirect_to :back 
    end 

    private 

    # If your tag model looks like: Tag(id: integer, inputtag: string) 
    def tag_params 
    params.require(:tag).permit(:inputtag) 
    end 
相關問題