0

私はレールに不慣れで、非常に単純な問題だと思うものを抱えています。

「タスク」のリストがあります。タスクをクリックすると、データベース内の行を更新して、タスクを完了としてマークします(「ステータス」列を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>

レーキルートの編集は次のことを示しています。

       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
4

1 に答える 1

1

これをroutes.rbに入れます:

resources :tasks do
  member do
    get :change
  end
end

change_taskタスク ID を渡すヘルパー パスを追加します。
リンクを次のように変更します。

<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')) %>"
);

このようにすると、partial( _tasks_list.html.erb) が必要になることに注意してください。

于 2012-11-13T18:00:02.167 に答える