Denis Policarpo
Denis Policarpo

Reputation: 153

Send items to another page by click button

I have a small task registry system where the user can create, edit and delete their tasks. I'm trying to create a "Task Completed" button that when clicked moves the given task to another page.

Below is my controller, view and routes:

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

Below is my view:

<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>

My routes:

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

Upvotes: 0

Views: 182

Answers (1)

max
max

Reputation: 102134

Basically you can create such a button without adding any additional routes or actions by using the update action:

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

Provided tarefa has been persisted this will send a PATCH request to /tarefas/:id.

If you really need a different response then the standard update then add a custom action. But don't use GET as GET requests should be idempotent (not alter resources). Instead you want to use PATCH or 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

Upvotes: 1

Related Questions