参数丢失或值是空的:ad



我有一个包含标题

的属性的" ads"表

ads_controller.rb

def index
    if params[:title]
   @ad = Ad.where('title LIKE ?', "%#{params[:title]}%")
 else
   @ads = Ad.published.paginate(page: params[:page], per_page: 5)
 end
    @categories = Category.all
    @categorytypes = Categorytype.all
  end

application.html.erb

  <%= form_tag(ads_path method: :get) do %>
             <%= text_field_tag :title, params[:title], class: 'form-control' %>
             <button type="submit" class="btn btn-default btn-xs"><i class="fa fa-search"></i></button>
            <% end %>

提交时,我会收到一条错误消息:

参数丢失或值是空的:AD def ad_params params.require(:ad).permit(:title, :description, :price, :location, :category_id, images: []) end 有什么解决方案?

当您提交表单时,您不会使用适当的参数类型提交。在您的控制器中,由于ad_params方法,您期望它以params[:ad][:title]的格式。但是您当前将参数作为params[:title]获取。这就是您面对这个问题的原因。

现在来解决方案,您可以更改形式的文本字段标签,如以下方式:

<%= text_field_tag "ad[title]", nil, class: "form-control" %>

最新更新