我正在添加查询展开,但是在添加代码时,我收到First argument in form cannot contain nil or be empty
错误,指向行<%= form_for @search, :class => 'content' do |f| %>
我使用用户更新帖子并搜索控制器以便更好地查看。
index.html.rb:
<%= render 'searches/new' %>
/searches/_new.html.erb:
<%= form_for @search do |f| %>
<div class="field">
<%= f.label :gender %><br />
<%= f.select :gender, ['male', 'female'], :include_blank => true %>
</div>
<div class="field">
<%= f.label :state %><br />
<%= f.text_field :state %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
原始源代码:
<div class="layer1">
<p class="heading">Header-1 </p>
<div class="content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit orem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
用户控制器:
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
答案 0 :(得分:1)
错误:
表单中的第一个参数不能包含nil或者为空的错误 指向该行&lt;%= form_for @search,:class =&gt; '内容'做| f | %GT;
表示@search
为零。在加载此视图之前,请务必在控制器的相关操作中进行设置。由于您尚未共享Controller特定代码,因此难以确定准确位置以设置@search
实例变量。
<强>更新强>
在UsersController #index
中添加@search
def index
@users = User.all
@search = Search.new
end
将views/users/index.html.erb
中的渲染调用更新为
<%= render partial: 'searches/new', locals: {search: @search} %>
此外,在部分views/users/searches/_new.html.erb
更新form_for
<%= form_for search do |f| %>