2017-04-25 53 views
0

我有一個小任務註冊表系統,用戶可以在其中創建,編輯和刪除他們的任務。我試圖創建一個「任務完成」按鈕,單擊時將給定的任務移動到另一個頁面。通過點擊按鈕將項目發送到另一頁

下面是我的控制器,視圖和路線:

class TarefasController < ApplicationController 

    before_filter :authenticate_user! 

    def index 
     @tarefa = current_user.tarefas.all 
    end 

    def show 
     @tarefa = Tarefa.find(params[:id]) 
    end 

    def new 
     @tarefa = Tarefa.new 
    end 

    def edit 
     @tarefa = current_user.tarefas.find_by(id: params[:id]) 
    end 

    def create 
     @tarefa = current_user.tarefas.new(tarefa_params) 
     if @tarefa.save 
      redirect_to @tarefa 
     else 
      render 'new' 
     end 
    end 

    def update 
     @tarefa = current_user.tarefas.find_by(id: params[:id]) 
     if @tarefa.update(tarefa_params) 
      redirect_to @tarefa 
     else 
      render 'edit' 
     end 
    end 

    def destroy 
     @tarefa = current_user.tarefas.find_by(id: params[:id]) 
     @tarefa.destroy 

     redirect_to tarefas_path 
    end 


    private 
     def tarefa_params 
     params.require(:tarefa).permit(:titulo, :descricao, :data, :time) 
     end 


end 

下面是我的看法:

<div class="row container-fluid"> 
<br><br><br><br> 
    <div class="col-md-8 col-md-offset-2"> 


     <div class="panel panel-info "> 

     <div class="panel-heading"><h3>Lista de tarefas</h3></div> 

      <div class="panel-body"> 


       <button type="button" class="btn btn-default"><%= link_to 'Nova Tarefa', new_tarefa_path %></button> 

      <div class="table table-responsive"> 
       <table class="table table-bordered"> 
        <tr> 
        <th>Titulo</th> 
        <th>Descrição</th> 
        <th>Data e Hora</th> 
        <th>Cronometro</th> 
        <th>Estado da Tarefa</th> 
        <th colspan="3"></th> 
        </tr> 

        <% @tarefa.each do |tarefa| %> 
        <tr> 
         <td><%= tarefa.titulo %></td> 
         <td><%= tarefa.descricao %></td> 
         <td><%= tarefa.data %></td> 
         <td><%= timeago_tag tarefa.created_at, :nojs => true, :limit => 10.days.ago %></td> 
         <td><button type="button" class="btn btn-default"><%= link_to 'Mostrar', tarefa_path(tarefa) %></button></td> 
         <td><button type="button" class="btn btn-default"><%= link_to 'Editar', edit_tarefa_path(tarefa) %></button></td> 
         <td><button type="button" class="btn btn-default"><%= link_to 'Apagar', tarefa_path(tarefa), method: :delete, data: { confirm: 'Tem certeza?'} %></button></td> 
        </tr> 
        <% end %> 
       </table> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 

</div> 

我的路線:

Rails.application.routes.draw do 
    devise_for :users 
    # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 
    resources :tarefas 
    match 'tarefas/tarefascompletas' => 'tarefas#completedtask', via: 'get' 
    root 'home#index' 
end 
+3

有什麼問題嗎? – idej

+0

我不知道如何創建這個點擊時發送的按鈕只發送完整的任務到另一個頁面。 –

回答

1

基本上你可以不建立這樣一個按鈕通過使用update操作添加任何其他路由或操作:

<%= form_for(tarefa) do |f| %> 
    <%= f.hidden_field :complete, value: true %> 
    <%= f.submit 'Mark as complete' %> 
<% end %> 

提供的tarefa已被保留,這將發送一個PATCH請求到/tarefas/:id

如果你確實需要不同的響應,那麼標準更新然後添加一個自定義操作。但不要使用GET,因爲GET請求應該是冪等的(而不是改變資源)。相反,你想使用PATCH或PUT。

resources :tarefas do 
    member do 
    patch :complete 
    end 
end 

<%= button_to 'Mark as complete', complete_tarefa(tarefa), method: :patch %> 

# PATCH /tarefas/:id/complete 
def complete 
    @tarefa = current_user.tarefas.find_by(id: params[:id]) 
    if @tarefa.update(complete: true) 
    redirect_to @tarefa 
    else 
    render 'edit' 
    end 
end 
+0

在使用'current_user.tarefas.find_by'作爲授權方法的一個附註中存在缺陷,因爲它會給出404 - Not Found響應,而不是指示用戶未被授權的正確響應。改用CanCanCan或Pundit。 – max

+0

謝謝你的幫助!但是當我點擊「標記爲完成」按鈕時,我收到一條錯誤消息。消息是:任務的未知屬性「完成」。 你有什麼想法可能是什麼? 謝謝! –

+0

我剛剛展示了一個如何去做的例子。不是複製粘貼解決方案。您需要實際擁有一個數據庫列,以跟蹤您要更新的狀態。 – max