Max
Max

Reputation: 275

ruby on rails 4 - multiple forms for same model on one page

In my application there is a User-model and a Task-model. The user can create tasks and manage them. But now I want the users to be able to fill in the forms of multiple task on one page.

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"

But this doesn't work obviosly. I get the following error:

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/taskr4

Application 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:intimes' app/views/tasks/new.html.haml:1:in >`_app_views_tasks_new_html_haml__991073273_86859300'

Upvotes: 2

Views: 1640

Answers (2)

user2130345
user2130345

Reputation: 56

What's your exception backtrace? from your post, I think there should have any error.

By the way, if you want create or edit multiple tasks, please take a look at 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

Upvotes: 2

Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 14039

There is no simple way in Rails to instanciate several times the same standalone model without reworking your controller and your view. However, if your User model has_many :tasks then you can take advantage of nested forms/attributes which will let you create forms where you can add several tasks to a user.

There are several gems which make this trivial. For example you might want to have a look at what you can do with the cocoon gem.

However if you really want to have a standalone form to instanciate several tasks, eg. for different users, then I'm afraid you'll have extra work, which is not that easy to figure out. Basically you need to define new_multiple and create_multiple actions in your controller, and figure out how to write your HTML view so when you POST it, the parameters are interpreted as an array of tasks. ie you want params[:tasks] to return an array in your controller, so you can do something like params[:tasks].each {|task_params| Task.create(task_params) }

Upvotes: 0

Related Questions