Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
What is supposed to handle the HTML attribute required: true? Is it conventional HTML or are you using a client-side framework to "trigger" an event handling the non-presence of required values? –  MrYoshiji Jun 11 at 20:26
    
This attribute specifies that the user must fill in a value before submitting a form. It cannot be used when the type attribute is hidden, image, or a button type (submit, reset, or button). The :optional and :required CSS pseudo-classes will be applied to the field as appropriate. –  Octopus-Paul Jun 11 at 20:35

2 Answers 2

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 */
}
share|improve this answer

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  %>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.