Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 表单中的第一个参数不能包含nil_Ruby On Rails_Ajax_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 表单中的第一个参数不能包含nil

Ruby on rails 表单中的第一个参数不能包含nil,ruby-on-rails,ajax,ruby-on-rails-4,Ruby On Rails,Ajax,Ruby On Rails 4,我尝试使用Ajax请求来创建一个新对象,效果非常好。在我的视图文件中,我曾为link\u to设置remote:true选项,一切正常 现在,我想在加载视图文件时不使用链接,而只使用渲染我的_表单部分。 我不明白为什么我会犯这个错误。谁能告诉我我做错了什么 视图/任务/index.html <h3>Tasks database</h3> <table> <% @tasks.each do |task| %> <tr class='task

我尝试使用Ajax请求来创建一个新对象,效果非常好。在我的视图文件中,我曾为link\u to设置
remote:true
选项,一切正常

现在,我想在加载视图文件时不使用链接,而只使用
渲染我的_表单部分。 我不明白为什么我会犯这个错误。谁能告诉我我做错了什么

视图/任务/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>
<%= 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 %>
任务数据库
控制器/任务\u 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
class TasksController
视图/任务/_form.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>
<%= 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 %>

视图/任务/create.js

$('#new_task').remove();
$('#new_link').show();
$('.tasks').last().after("<%=j render partial: 'task', :locals => { :task => @task } %>");
setXeditable();
$('new#u task')。删除();
$(“#新链接”).show();
$('.tasks').last()之后(“{:task=>@task}%>”;
setXeditable();
这是我的耙路线

有什么帮助吗

表单中的第一个参数不能包含nil或为空

您正在
索引
页面中呈现
表单
,但未在
索引
方法中设置
@task

您应该在
索引
方法中设置
@task

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

事实证明我不再需要新的行动了?@AlexanderShmatko是的。在您的情况下不需要它。更喜欢使用
@task=current\u user.tasks.build
它应该将
user\u id
填入
@task
实例另一种方式直接为[current\u user,current\u user.tasks.build]在表单中为[current\u user,current\u user.tasks.build]构建实例