Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - adding CSS to text_field

I created a form in Rails and when I add a CSS class to a text_field the form will not submit.

I am still new to rails so any help would be much appreciated.

This is my form:

<% @page_title = "Contact" %>
    <hr>
    <div class="col-lg-12">
                <h1 class="page-header">
                    BK Legal Courier Corp <br />Call BK @ <span style="color:#47B224">613-286-8265</span>
                </h1>
            </div>

<hr>
<div class="container">
  <h2>Contact Us</h2>
   <%= form_for :fc, :url => {:action => 'create'} do |f| %>
    <div class="form-group">
      <%= f.label :first_name %>
      <%= f.text_field (:first_name, :class => 'form-control')%>
    </div>
    <div class="form-group">
      <%= f.label :last_name %>
     <%= f.text_field (:last_name, :class => 'form-control') %>
    </div>
     <div class="form-group">
      <%= f.label :email %>
      <%= f.text_field (:email, :class => 'form-control') %>
    </div>
     <div class="form-group">
  <%= f.label :comment %>
 <%= f.text_area (:comment, :class => 'form-control') %>
</div>
<div class="form-group">
  <%= f.label :referral %>
        <%= f.select (:referral, [['Friends', 1], ['News', 2], ['Online', 3], ['Other', 4]], :class => 'form-control')%>
</div>
 <%= submit_tag ("Submit") %>
  </form>
</div>

<% end %>
</body>

</html>

The problem is that my form will only submit without the :class => "form-control" in it. I must have brackets around each of the form field variables.

This is my routes.rb file:

    Rails.application.routes.draw do
  root "home#index"


  match ':controller(/:action(/:id))', :via => [:get, :post]

    end

This is my controller:

    class HomeController < ApplicationController
     def home
        render("home/index")
     end
      def new
@fc = FeedbackCustomer.new
  end
  def create
     # Instantiate a new object using form parameters
    @fc = FeedbackCustomer.new(feedbackcustomer_params)
if @fc.save
    redirect_to(:action => "home")
end
  end

  def application

  end

private

def feedbackcustomer_params
params.require(:fc).permit(:first_name, :last_name, 
    :email, :comment, :referral)
    end
end

Any help would be much appreciated. Thanks!

like image 817
Evan Avatar asked Feb 10 '23 04:02

Evan


1 Answers

If you're using Ruby 1.9+ This should work:

<%= f.text_field :last_name, class: 'form-control' %>

If you're using a version that's older than 1.9 then this should work:

<%= f.text_field :last_name, :class => 'form-control' %>

For more information on this you can use the ActionView::Helpers::FormTagHelper API Documentation

The Form Helpers Rails Guide might be helpful to you as well.

like image 128
Andrew Hendrie Avatar answered Feb 12 '23 17:02

Andrew Hendrie