请帮助 我正在写我的ROR博客,并收到错误“参数缺失或值为空:文章”。
(Application Trace | Framework Trace | Full Trace
app/controllers/articles_controller.rb:55:in `article_params'
app/controllers/articles_controller.rb:31:in `search')
这是我的搜索方法和私有方法:
def search
if article_params[:search].blank?
@articles = Article.all
else
@articles = Article.search(params)
end
end
private
def article_params
params.require(:article).permit(:title, :text)
end
end
在这个主题ActionController::ParameterMissing in UsersController#create之后,我从以下方面改变了我的偏见:
<div class="form-inline mt-2 mt-md-0">
<%= form_tag search_articles_path, method: :get do %>
<%= text_field_tag :search, nil, placeholder: "Search", class: "form-control mr-sm-2" %>
<%= submit_tag "Search", class: "btn btn-light my-2 my-sm-0" %>
<% end %>
</div>
至:
<div class="form-inline mt-2 mt-md-0">
<%= form_for @article, search_articles_path, method: :get do |f| %>
<%= f.text_field :search, placeholder: "Search", class: "form-control mr-sm-2" %>
<%= f.submit "Search", class: "btn btn-light my-2 my-sm-0" %>
<% end %>
</div>
它也不起作用。
答案 0 :(得分:0)
您需要允许搜索参数,还要调用方法
def search
if article_params[:search].blank?
@articles = Article.all
else
@articles = Article.search(article_params)
end
end
def article_params
params.require(:article).permit(:title, :text, :search)
end