Alon Shmiel
Alon Shmiel

Reputation: 7121

redirect using a form

this form (views/workers/_form.html.erb) redirects me to the index of tasksadmins, after I push the 'create tasksadmin' button.

I want it to redirect me to "workers/index" and change the button to 'update the task'.

how can I do that please?

<%= form_for(@tasksadmin) do |f| %>
  <% if @tasksadmin.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@tasksadmin.errors.count, "error") %> prohibited this tasksadmin from being saved:</h2>

      <ul>
      <% @tasksadmin.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.hidden_field :admin_mail, :value => @tasksadmin.admin_mail %>

  <%= f.hidden_field :worker_mail, :value => @worker_mail %>

  <%= f.hidden_field :task, :value => @tasksadmin.task %>


<div class="field">
  <%= f.label :done %><br />
  <%= f.check_box :done %>
</div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

and this is my workers_controller:

class WorkersController < ApplicationController
  # GET /workers
  # GET /workers.json
  def index
    @tasks_worker = Tasksadmin.where(:worker_mail => "[email protected]")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @workers }
    end
  end

  # GET /workers/1
  # GET /workers/1.json
  def show
    @task_worker = Tasksadmin.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @worker }
    end
  end

  # GET /workers/new
  # GET /workers/new.json
  def new
    @worker = Worker.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @worker }
    end
  end

  # GET /workers/1/edit
  def edit
    @tasksadmin = Tasksadmin.find(params[:id])
    @worker_mail = "[email protected]"

  end

  # POST /workers
  # POST /workers.json
  def create
    @worker = Worker.new(params[:worker])

    respond_to do |format|
      if @worker.save
        format.html { redirect_to "@worker", notice: 'Worker was successfully created.' }
        format.json { render json: @worker, status: :created, location: @worker }
      else
        format.html { render action: "new" }
        format.json { render json: @worker.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /workers/1
  # PUT /workers/1.json
  def update
    @worker = Tasksadmin.find(params[:id])

    respond_to do |format|
      if @worker.update_attributes(params[:worker])
        format.html { render action: "index", notice: 'Worker was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @worker.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /workers/1
  # DELETE /workers/1.json
  def destroy
    @worker = Tasksadmin.find(params[:id])
    @worker.destroy

    respond_to do |format|
      format.html { render action: "index" }
      format.json { render json: @worker }
    end
  end
end

Upvotes: 2

Views: 5524

Answers (2)

nathanvda
nathanvda

Reputation: 50057

You create a form for an object @tasksadmin, which in your WorkersController#edit is set as follows:

@tasksadmin = Tasksadmin.find(params[:id]

This means that that parameter contains an object of class Tasksadmin, if you let rails build the path for the a Tasksadmin, it will send you to the TasksadminsController#update, and that is why your code does not work. You never get to the WorkersController#update. Check your logs to verify that.

Let me be very clear about this: you should not edit Tasksadmin objects in the WorkersController.

I do not understand why you would do that.

Upvotes: 0

sq1020
sq1020

Reputation: 1090

In your controller, you have to redirect to the appropriate path like so:

def update
  redirect_to "workers/index"
end

I would suggest using path helpers so if you have your routes set up as resources, you can do this:

redirect_to workers_path

As for changing the the button text, just change it to:

f.submit("update the task")

Upvotes: 4

Related Questions