简体   繁体   English

Ruby on Rails-使表单需要输入

[英]Ruby on Rails- Making input required for a form

I am trying to make this input in my search required to start a search. 我试图在我的搜索中输入此内容以开始搜索。 I tried :validate if there is input but that didn't work. 我尝试:validate是否有输入,但是没有用。 Any suggestions? 有什么建议么?

<%= text_field_tag "location", params[:location], :placeholder => 'City, zipcode, or address' %>

In whatever model the location belongs to, try adding this syntax: 无论位置属于哪种模型,请尝试添加以下语法:

class whatever < ActiveRecord::Base    
  validates :location, presence: true
end

make sure that location is part of attar_accessible in your model. 确保该位置是模型中attar_accessible的一部分。

change the syntax 更改语法

<%= text_field_tag "location", "#{params[:location]}", :placeholder => 'City, zipcode, or address' {:required => true} %>

and add this to your model 并将其添加到您的模型中

validates :location, presence: true 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM