ActionController::ParameterMissing in ArticlesController#search

help please I'm writing my ROR blog and receive error "param is missing or the value is empty: article".

(Application Trace | Framework Trace | Full Trace
app/controllers/articles_controller.rb:55:in `article_params'
app/controllers/articles_controller.rb:31:in `search')

Here is my search method and private method:

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

Also after this topic ActionController::ParameterMissing in UsersController#create I changed peace of my partial from:

        <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>

to:

<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>

And it also doesn't work.

1 answer

  • answered 2019-02-10 13:12 lacostenycoder

    You need to permit the search param but also call the method

    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