【问题标题】:param is missing or the value is empty: ad参数缺失或值为空:ad
【发布时间】:2019-09-28 03:28:13
【问题描述】:

我有一个包含标题属性的“广告”表

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 有什么解决办法吗?

【问题讨论】:

  • 我不确定这个&lt;%= form_tag(ads_path method: :get) do %&gt; 不会引发某种语法错误,但请发布此方法实际生成的表单。

标签: ruby-on-rails search ruby-on-rails-5


【解决方案1】:

当您提交表单时,您没有使用正确的参数类型提交它。在您的控制器中,由于 ad_params 方法,您期望它采用 params[:ad][:title] 的格式。但是您当前获取的参数为 params[:title] 。所以这就是你面临这个问题的原因。

现在解决方案,您可以将表单的文本字段标记更改为:

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

【讨论】:

  • 我不知道为什么,但是当我点击提交时,它重定向我创建方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多