简体   繁体   中英

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)

Syntax

As pointed out in the comments, you have a series of problems with your code syntax, specifically with submit_tag & text_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 is cascading style sheets - meaning they're only meant to style your page. They can't fix any syntax, backend or HTML rendering issues - only how the HTML appears in the browser

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:

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

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

Does your form code generate a valid HTML? As far as I see from documentation , text_field_tag method has three arguments:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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