2012-11-13 84 views
0

我是新來的鐵軌,我有我認爲是一個非常簡單的問題。Rails幫助方法問題

我有一個「任務」列表。當你點擊一個任務時,我想更新它在數據庫中的行以將其標記爲完整(將「狀態」列從0更改爲1)。

這裏是鏈接的樣子在我看來:

<td><%= link_to t.name, change_task_status_path(:id => t.id) %> 

這裏是我的tasks_controller.rb什麼:

def change_task_status 
    @t = Task.find_by_id(params[:id]) 
    @t.status = '1' # 1 = complete 
    @t.save 
    render :nothing => true 
end 

我無法弄清楚如何格式化鏈接正確!加載視圖時,我得到這個錯誤:

undefined method `change_task_status_path' for #<#<Class:0x3a6c144>:0x3a69d54> 

編輯 耙路線顯示:

resources :tasks do 
    member do 
    get :change 
    end 
end 

它將:

 tasks GET /tasks(.:format)    tasks#index 
      POST /tasks(.:format)    tasks#create 
    new_task GET /tasks/new(.:format)   tasks#new 
    edit_task GET /tasks/:id/edit(.:format) tasks#edit 
     task GET /tasks/:id(.:format)   tasks#show 
      PUT /tasks/:id(.:format)   tasks#update 
      DELETE /tasks/:id(.:format)   tasks#destroy 
     phases GET /phases(.:format)   phases#index 
      POST /phases(.:format)   phases#create 
    new_phase GET /phases/new(.:format)  phases#new 
    edit_phase GET /phases/:id/edit(.:format) phases#edit 
     phase GET /phases/:id(.:format)  phases#show 
      PUT /phases/:id(.:format)  phases#update 
      DELETE /phases/:id(.:format)  phases#destroy 
    projects GET /projects(.:format)   projects#index 
      POST /projects(.:format)   projects#create 
new_project GET /projects/new(.:format)  projects#new 
edit_project GET /projects/:id/edit(.:format) projects#edit 
    project GET /projects/:id(.:format)  projects#show 
      PUT /projects/:id(.:format)  projects#update 
      DELETE /projects/:id(.:format)  projects#destroy 
+0

運行'rake routes'來查看你的路由名稱實際上是什麼。 – MrDanA

+0

我編輯我的問題以顯示我的耙路線響應。這條道路似乎根本沒有出現。 – nathan

+0

試着看看這個鏈接瞭解更多信息:http://guides.rubyonrails.org/routing.html。當您添加控制器操作時,您不會自動訪問它。你必須在你的routes.rb文件中定義它。然後,根據你如何定義它,它將決定你的助手鍊接的樣子。 – MrDanA

回答

1

在你的routes.rb將這個添加傳遞任務ID的助手路徑change_task
並改變你的鏈接到本:

<td><%= link_to t.name, change_task_path(:id => t.id) %> 

而且控制器:

def change 

編輯:

爲了使Ajax調用,你這樣做是正確,加:remote => true你的鏈接像這樣:

<%= link_to t.name, change_task_path(:id => t.id), :remote => true %>  

這樣,響應預計您的控制器將採用js格式。

def change 
    # do your thing 
    respond_to do |format| 
    format.js 
    end 
end 

當你這樣做,你預計將有上你的意見文件夾中的文件change.js.erb使所有更改的頁面。事情是這樣的:

$('#tasks_list').children().remove(); 
$('#tasks_list').html(
"<%= j(render('tasks_list')) %>" 
); 

請記住,如果你做的事情這樣,您將需要一個部分(_tasks_list.html.erb)。

+0

我是否還需要更新我的tasks_controller.rb? – nathan

+0

是的,將方法的名稱更改爲「def change」。忘了那個,對不起。 =] – MurifoX

+0

你是男人!謝謝!!最後一個請求。有沒有一種簡單的方法來使這個在後臺運行的ajax調用?我知道有一些關於使用:remote => true,但我不確定鏈接格式的位置。 – nathan