Rails:如何强制表单发布而不是修补? [英] Rails: How to force a form to post rather than patch?
问题描述
项目/展览上的表单创建新任务的效果很好,但是,在项目/展示和任务/在任务/显示它想要编辑任务,因为我需要从我的任务控制器的show动作中调用任务ID。
我需要修改这个表单总是创建新的任务。
我试过 method:post
但是rails仍然插入< ; input type =hiddenname =_ methodvalue =patch/>
放入HTML中。
和形式:
pre $ code#任务控制器
def show
@projects = Project.all
@project = Project.find(params [:project_id])
@task = Task.find(params [:id])
@tasks = @ project.tasks.all
end
def创建
@project = Project.find(params [:project_id])
@task = @ project.tasks.new(task_params)
@ task.save
redirect_to @project
结束
#Form
<%= form_for([@ project,@task])do | f | %GT;
<%= f.text_field:title%>
<%= f.submit%>
<%end%>
有没有办法强制rails始终从这个表单创建新任务?
尝试使用现有任务的属性分配新任务对象。
#任务控制器
def show
@projects = Project.all
@project = Project.find(params [:project_id])
@task = Task.find(params [:id])
@new_task = Task.new(title: @ task.title)
@tasks = @ project.tasks.all
结束
#...
#Form
< b< ;%= form_for([@ project,@new_task])do | f | %GT;
<%= f.text_field:title%>
<%= f.submit%>
<%end%>
I have a form for creating tasks which is displayed on both projects/show and tasks/show.
The form on projects/show creates new tasks fine, but on tasks/show it wants to edit the task, since I need to call the task ID from within the show action of my tasks controller.
I need to modify this form to always create new tasks.
I tried method: post
but rails still inserts <input type="hidden" name="_method" value="patch" />
into the HTML.
Here are my controllers and the form:
# Tasks Controller
def show
@projects = Project.all
@project = Project.find(params[:project_id])
@task = Task.find(params[:id])
@tasks = @project.tasks.all
end
def create
@project = Project.find(params[:project_id])
@task = @project.tasks.new(task_params)
@task.save
redirect_to @project
end
#Form
<%= form_for([@project, @task]) do |f| %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
Is there any way to force rails to always create new tasks from this form?
Try assigning a new task object using the existing task's attributes. That way the form will POST rather than PATCH:
# Tasks Controller
def show
@projects = Project.all
@project = Project.find(params[:project_id])
@task = Task.find(params[:id])
@new_task = Task.new(title: @task.title)
@tasks = @project.tasks.all
end
# ...
#Form
<%= form_for([@project, @new_task]) do |f| %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
这篇关于Rails:如何强制表单发布而不是修补?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!