2

在我按下“创建任务管理员”按钮后,此表单 (views/workers/_form.html.erb) 将我重定向到任务管理员的索引。

我希望它将我重定向到“工人/索引”并将按钮更改为“更新任务”。

请问我该怎么做?

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

这是我的workers_controller

class WorkersController < ApplicationController
  # GET /workers
  # GET /workers.json
  def index
    @tasks_worker = Tasksadmin.where(:worker_mail => "alon.shmiel@gmail.com")

    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 = "alon.shmiel@gmail.com"

  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
4

2 回答 2

4

在您的控制器中,您必须像这样重定向到适当的路径:

def update
  redirect_to "workers/index"
end

我建议使用路径助手,所以如果您将路线设置为资源,您可以这样做:

redirect_to workers_path

至于更改按钮文本,只需将其更改为:

f.submit("update the task")
于 2012-12-26T23:19:47.417 回答
0

你为一个对象创建一个表单@tasksadmin,它在你WorkersController#edit的设置如下:

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

这意味着该参数包含 class 的对象Tasksadmin,如果您让 rails 为 a 构建路径Tasksadmin,它会将您发送到TasksadminsController#update,这就是您的代码不起作用的原因。你永远不会到达WorkersController#update. 检查您的日志以验证这一点。

让我非常清楚这一点:您不应该TasksadminWorkersController.

我不明白你为什么要那样做。

于 2012-12-27T01:10:39.507 回答