Alex Nikolsky
Alex Nikolsky

Reputation: 2179

First argument in form cannot contain nil

I tried to use Ajax request for creating a new object and this had been working very well. In my view file I used to set remote: true option for link_to and everything was fine.

Now I want to render my _form partial once the view file is loaded without a link, but just using a <%= render 'form' %>. I don't understand why I am getting this error. Who can enlighten me what am I doing wrong?

enter image description here

views/tasks/index.html

<h3>Tasks database</h3>

<table>
<% @tasks.each do |task| %>
<tr class='tasks' id="task_<%= task.id %>">
  <td>
    <%= link_to user_task_path(current_user, task.id), method: :delete,
     data: { confirm: 'Are you sure?' }, remote: true do %>
      <i class="glyphicon glyphicon-trash"></i>
    <% end %>

    <a href="#" data-xeditable="true" data-pk="task" data-model='task' data-name='title'
      data-url="/users/<%= current_user.id %>/tasks/<%= task.id %>" data-title="Enter title">
      <i><%= task.title %></i>
    </a>

  </td>
</tr>
<% end %>
<tr>
  <td><%= render 'form' %></td>
</tr>

</table>

controllers/tasks_controller.rb

class TasksController < ApplicationController
before_filter :authorize, only: [:edit, :new, :destroy]

  def index
    @tasks = current_user.tasks
  end

  def show
    @task = Task.find(params[:id])
    redirect_to users_path
  end

  def edit
    @task = Task.find(params[:id])
  end

  def new
    @task = Task.new
  end

  def create
    @task = current_user.tasks.new(task_params)
    respond_to do |format|
    if @task.save
      format.html { redirect_to user_tasks_path(current_user) }
      format.js
    else
      format.html { render action: 'new' }
      format.js
    end
  end
 end

  def update
    @task = Task.find(params[:id])

      respond_to do |format|
    if @task.update(task_params)
      format.html { redirect_to user_tasks_path(current_user), notice: 'Post successfully was updated.'}
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { head :no_content }
    end
  end
 end

  def destroy
    @task = Task.find(params[:id])
    @task.destroy
    respond_to {|format| format.js }
  end

  private

    def task_params
     params.require(:task).permit(:title)
    end
 end

views/tasks/_form.html

<%= form_for [current_user, @task], remote: true  do |f| %>
  <%= f.text_field :title %>
  <%= f.hidden_field :user_id, value: params[:user_id] %>
  <%= f.submit %>
<% end %>

views/tasks/create.js

$('#new_task').remove();
$('#new_link').show();
$('.tasks').last().after("<%=j render partial: 'task', :locals => { :task => @task } %>");
setXeditable();

Here is my rake routes enter image description here

Any help?

Upvotes: 1

Views: 35

Answers (1)

Pavan
Pavan

Reputation: 33542

First argument in form cannot contain nil or be empty

You are rendering form in index page and you didn't set @task in index method.

You should set @task in index method

def index
  @tasks = current_user.tasks
  @task = Task.new
end

Upvotes: 3

Related Questions