在我的应用程序中有一个用户模型和一个任务模型。用户可以创建任务并管理它们。但现在我希望用户能够在一页上填写多个任务的表单。
tasks_controller.rb
class TasksController < ApplicationController
before_action :find_task, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def new
@task = current_user.tasks.build
end
def create
@task = current_user.tasks.build(task_params)
if @task.save
redirect_to @task, notice: "Task erstellt"
else
render 'new'
end
end
def edit
end
def update
if @task.update(task_params)
redirect_to @task
else
render 'edit'
end
end
def destroy
@task.destroy
redirect_to root_path, notice: "Task gelöscht"
end
def show
end
def index
@tasks = Task.all.order("created_at DESC")
end
private
def task_params
params.require(:task).permit(:name, :description, :urgency, :importance, :time)
end
def find_task
@task = Task.find(params[:id])
end
end
new.html.haml
- 6.times do
= form_for @task do |t|
= t.text_field :name
%br
= t.text_area :description
%br
= t.text_field :urgency
%br
= t.text_field :importance
%br
= t.text_field :time
%br
= t.submit "Bestätigen"
但这显然不起作用。我收到以下错误:
ArgumentError in Tasks#new
Showing C:/Sites/taskr4/app/views/tasks/new.html.haml where line #13 raised:wrong number of arguments (0 for 1..2)
Rails.root: C:/Sites/taskr4Application Trace | Framework Trace | Full Trace app/views/tasks/new.html.haml:13:in
block in >_app_views_tasks_new_html_haml__991073273_86859300' app/views/tasks/new.html.haml:1:in
times' app/views/tasks/new.html.haml:1:in >`_app_views_tasks_new_html_haml__991073273_86859300'
最佳答案
你的异常回溯是什么?从你的帖子来看,我认为应该有任何错误。
顺便说一句,如果您想创建或编辑多个任务,请查看http://railscasts.com/episodes/198-edit-multiple-individually 。
<% form_tag create_multiple_tasks_path, :method => :put do %>
<% 6.times do %>
<% fields_for "tasks[]", Task.new do |f| %>
<h2><%=h task.name %></h2>
<%= render "fields", :f => f %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
def create_multiple
@tasks = params[:tasks].map do |task_params|
Project.new(task_params).save
# TODO: add validate and error messages.
end
end
关于ruby-on-rails - ruby on Rails 4 - 一页上同一模型的多个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35536020/