简体   繁体   English

Ruby on Rails:必需的输入字段CSS不显示

[英]Ruby on Rails: Required input field CSS not showing

I have a form and I'm setting a field to be required before submitting, however nothing is showing up when I hit the Search button. 我有一个表单,并且在提交前设置了必填字段,但是当我按下“搜索”按钮时,什么都没有显示。 What do I need to do to style the form? 我需要做什么来样式化表单?

<%= form_tag search_path, :method => :get, class: "form-search-home" do %>

  <%= text_field_tag :q, :class => "term form-control" %>
  <%= text_field_tag :loc, :class => "loc form-control", :required => true  %>

  <%= button_tag :type => :submit, :class => "btn" do %>Search<% end %>

<% end %>

Thanks! 谢谢!

nothing is showing up when I hit the Search button 当我按下“搜索”按钮时,什么都没有显示

The problem here is likely a Rails / HTML issue than CSS (as mentioned in your question) 这里的问题可能是CSSRails / HTML问题(如您的问题所述)

Syntax 句法

As pointed out in the comments, you have a series of problems with your code syntax, specifically with submit_tag & text_field_tag : 正如评论中指出的那样,您的代码语法存在一系列问题,尤其是submit_tagtext_field_tag

<%= form_tag search_path, method: :get, class: "form-search-home" do %>

  <%= text_field_tag :q, nil, class: "term form-control" %>
  <%= text_field_tag :loc, nil, class: "loc form-control", required: true  %>

  <%= submit_tag "Search", class: "btn" %>

<% end %>

This should fix any of the syntax issues you have on your form, allowing it to submit. 这应该可以解决表单上的任何语法问题,并允许其提交。 The reason why it doesn't at the moment is likely down to the syntax being incorrect. 目前不支持的原因可能是语法不正确。 If you use the above code, it should render the form correctly, allowing you to submit it as required! 如果使用上面的代码,它应该正确呈现表单,允许您按要求提交!

-- -

CSS CSS

CSS is cascading style sheets - meaning they're only meant to style your page. CSS是cascading style sheets -这意味着它们仅用于样式化您的页面。 They can't fix any syntax, backend or HTML rendering issues - only how the HTML appears in the browser 他们无法解决任何语法,后端或HTML呈现问题-仅解决HTML在浏览器中的显示方式

If you've still got trouble with your CSS, you'll be best styling the form with the inputs inheriting from the main class styling: 如果您在使用CSS时仍遇到问题,最好使用从主类样式继承的输入来对表单进行样式设置:

#app/assets/stylesheets/application.css
form {
   /* form code */
}

form input.required {
   /* required form element styling */
}

Does your form code generate a valid HTML? 您的表单代码是否生成有效的HTML? As far as I see from documentation , text_field_tag method has three arguments: 据我从文档中看到,text_field_tag方法具有三个参数:

text_field_tag(name, value = nil, options = {})

Your example ommits the second argument (value), so may be that is the case. 您的示例省略了第二个参数(值),可能就是这种情况。 Wonder if this could help: 想知道这是否可以帮助您:

<%= text_field_tag :loc, nil, :class => "loc form-control", :required => true  %>

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

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