Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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 - Fatal编程技术网

Ruby on rails 参数错误对于搜索表单,表单中的第一个参数不能包含nil或为空

Ruby on rails 参数错误对于搜索表单,表单中的第一个参数不能包含nil或为空,ruby-on-rails,Ruby On Rails,我正在添加一个查询折叠,但是在添加代码时,我得到一个表单中的第一个参数不能包含nil或为空错误,该错误指向行“content”do | f |%> 我用用户和搜索控制器更新了帖子,以获得更好的外观 index.html.rb: <%= render 'searches/new' %> 搜索控制器: def settings @user = User.find(params[:id]) end def new @user = User.new en

我正在添加一个查询折叠,但是在添加代码时,我得到一个
表单中的第一个参数不能包含nil或为空
错误,该错误指向行
“content”do | f |%>

我用用户和搜索控制器更新了帖子,以获得更好的外观

index.html.rb:

 <%= render 'searches/new' %>
搜索控制器:

  def settings
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end

    def edit
      @user = User.find(params[:id])
end

  def index
    @users = User.all
  end
  def new
    @search = Search.new
  end

  def create
    @search = Search.new(params[:search])
    if @search.save
      redirect_to @search
    else
      render 'new'
    end
  end

  def show
    @search = Search.find(params[:id])
    @users = @search.users
  end

end
错误:

表单中的第一个参数不能包含nil或为空,错误为 指向行“content”do | f |%>

表示
@search
为零。在加载此视图之前,请确保在控制器的相关操作中进行设置。由于您没有共享特定于控制器的代码,因此很难确定要设置
@search
实例变量的确切位置

更新

在UsersController索引中添加
@search

views/users/index.html.erb
中的渲染调用更新为

<%= form_for search do |f| %>

另外,在部分
视图/users/searches/_new.html.erb
中,将
表单更新为

<%= form_for search do |f| %>


搜索表单位于搜索控制器下。然而,我是从用户控制器工作。当切换代码以呈现表单时(我将/searches/new.html.erb切换为partial),我得到了相同的错误。因此,即使在渲染中,我也必须在show action下调用@search?是的,您需要在show action中设置
@search
,以便它在partial中可用。您认为理想的做法是什么?我尝试了
@search=search.new
,但收到了相同的错误。如果你有时间看的话,我会发布用户控制器。你在哪里发布的?我在问题中没有看到。谢谢分享。我看到了。UsersController的哪一个操作呈现部分内容?这就是您需要设置
@search
的地方。
  def index
    @users = User.all
    @search = Search.new
  end
<%= form_for search do |f| %>