我正在实现一个包含两个提交按钮的Rails表单,如下所示:
<%= f.submit 'Save and Add New Task' %>
<%= f.submit 'Save and Finish' %>
这是处理请求并指向正确操作的代码:
def create_split
byebug
define_task
respond_to do |format|
new_task = @task #saves in new_task the task created by the user
load_new_task_attributes #loads everything needed for generating a new form for creating new tasks in this split
opr = InstantiationOperator.find_by name: "split"
@operator = opr.name
@operator_id = opr.id
@iteration = Iteration.find(new_task.iteration_id)
@project = @iteration.project
@instances = @iteration.project.instances
@activity = Activity.find(task_activity_params[:activity_id])
if new_task.save
if params[:newtask]
format.html {render :split, notice: 'Task successfully created. Go ahead and add a new one'}
elsif params[:finish]
format.html { redirect_to new_task.iteration, notice: 'Task successfully created.' }
end
else
format.html {render :split}
end
end
end
但它始终会转到else
块。
我使用了byebug并发现params[:commit]
正在获得nil
。
然后,我尝试了另一种方法:
<%= f.submit 'Save and Add New Task', name: 'newtask' %>
<%= f.submit 'Save and Finish', name: 'finish' %>
和
if params[:newtask]
format.html {render :split, notice: 'Task was successfully created. Go ahead and add a new one'}
else
format.html { redirect_to new_task.iteration }
end
同样,它只会进入else
块。
我用byebug检查了params[:newtask]
,它的值也是nil
。
我认为它必须简单,但我还没有在互联网上发现任何相关内容。
提前致谢
编辑:
更多控制器详细信息:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
def create_split
byebug
define_task
respond_to do |format|
new_task = @task #saves in new_task the task created by the user
load_new_task_attributes #loads everything needed for generating a new form for creating new tasks in this split
opr = InstantiationOperator.find_by name: "split"
@operator = opr.name
@operator_id = opr.id
@iteration = Iteration.find(new_task.iteration_id)
@project = @iteration.project
@instances = @iteration.project.instances
@activity = Activity.find(task_activity_params[:activity_id])
if new_task.save
if params[:newtask]
format.html {render :split, notice: 'Task successfully created. Go ahead and add a new one'}
elsif params[:finish]
format.html { redirect_to new_task.iteration, notice: 'Task successfully created.' }
end
else
format.html {render :split}
end
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
@task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:name, :description, :priority, :category, :status, :startDate, :comments, :TTC, :iteration_id,:instance_id, task_artifacts_attributes: [:id, :name, :description, :_destroy])
end
#This is called before any task creation method is started
def define_task
@task = Task.new(task_params)
@task.endDate = calculate_endDate(@task)
@task.task_activities << TaskActivity.new(activity_id: task_activity_params[:activity_id], instantiation_operator_id: task_activity_params[:instantiation_operator_id])
@task.task_actor_roles << TaskActorRole.new(task_role_id: task_actor_role_params[:task_role_id] , task_actor_id: task_actor_role_params[:task_actor_id] )
end
end
整体观点:
<ol class="breadcrumb">
<li>1- Select project, iteration and operator</li>
<li class="active">2- Create task - split operator</li>
</ol>
<h1>New Task - Split Operator</h1>
<%= render 'task_form_header' %>
<p>Activity: <%= @activity.name %></p>
<div class="col-md-12">
<%= form_for(@task, url: {action: 'create_split'}, html: {class: "task"}) do |f| %>
<div class="col-md-6"> <!-- this div will be closed inside the form fields partial !-->
<%= hidden_field :task_activity, :activity_id, :value => @activity.id %>
<%= hidden_field :task_activity, :instantiation_operator_id, :value => @operator_id %>
<%= hidden_field_tag :project, :project_id, :value => @project.id %>
<%= f.hidden_field :iteration_id, :value => @iteration.id %>
<div class="field form-group instance">
<%= label_tag :instance, "Select an instance" %>
<%= f.select :instance_id,@instances.collect{ |i| [i.name, i.id ]}, {prompt: "Select"}, id: "new_task_instance", required: true, class: "form-control" %>
</div>
<div class="field form-group">
<%= f.label :name %>
<%= f.text_field :name, class: "form-control" %>
</div>
<%= render 'form_fields', f: f %>
<div class="col-md-12 form-group">
<%= f.submit 'Save and Add New Task', name: 'newtask', class: "btn btn-default submit-task" %>
<%= f.submit 'Save and Finish', name: 'finish', class: "btn btn-default submit-task" %>
<%= link_to 'Cancel', new_task_path, class: "btn btn-default" %>
</div>
<% end %>
</div>
答案 0 :(得分:0)
让你的表格像这样
<% form_for(something) do |f| %>
..
<%= f.submit 'Save and Add New Task' %>
<%= f.submit 'Save and Finish' %>
..
<% end %>
您的控制器操作应该与此代码段相似
def some_action
if params[:commit] == 'Save and Add New Task'
# your code goes here
elsif params[:commit] == 'Save and Finish'
# your code goes here
end
end
或强>
如果您使用名称为
的提交按钮 <%= f.submit 'Save and Add New Task', name: 'newtask' %>
<%= f.submit 'Save and Finish', name: 'finish' %>
您的控制器操作应该与此代码段相似
if params[:newtask]
# your code goes here
elsif params[:finish]
# your code goes here
end