Css bootstrap forms (rails newbie) - css

I was trying to add some styling into my user_form using css bootstrap.
This is how it looks at the minute:
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
And I was trying to put it in the form of:
<form>
<div class="form-group">
<label for="name">Name</label>
<input type="name" class="form-control" id="name" placeholder="Enter name">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
<div class="form-group">
<label for="exampleInputConfirmPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputConfirmPassword1" placeholder="Password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
My problem is that I dont know how to add the functionality to the css form. How can I do so?
Thanks in advance!
------ EDIT 1 -------
In order to clarify what is happening:
my _user_form.html.erb at the minute looks like the first set of code. However when I delete it and replace it with the second set of code nothing works.
Schema.rb
ActiveRecord::Schema.define(version: 20161210221810) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
end
end

Just add a class to the label:
<%= f.label :name, class: "my_class" %>

at the first you need to use that include your form items, and then in javascript send params to your backend! for example
<form action="demo_form.asp" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

Related

Adding a RoR registration script into a bootstrap modal

so i am trying to create a signup script within a Bootstrap Modal
but it fails out.
the same script works on a page of its won, just not sure why it doesnt work here.
is there something special about using bootstrap?
Here is the Error:
Below is the Modal Contents
<div class="modal fade" id="register-modal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Register</h4>
</div>
<%= form_for(#user) do |f| %>
<div class="modal-body">
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<%= f.text_field :first_name, :placeholder => "First name" %>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<%= f.text_field :last_name, :placeholder => "Last name" %> </div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<%= f.email_field :email, :placeholder => "Email" %>
</div>
</div>
<div class="form-group">
<label for="inputPassword" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<%= f.password_field :password, :placeholder => "Password" %>
</div>
</div>
<div class="modal-footer">
<%= f.submit "Create an account", class: "btn btn-primary btn-submit" %>
</div>
<% end %>
</div>
Here is my Users controller
class UsersController < ApplicationController
def new
#user = User.new
end
end
and the user model
class User < ApplicationRecord
has_secure_password
end
and finally my db:migrate file for my users
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :first_name
t.string :last_name
t.string :email
t.string :password_digest
t.timestamps
end
end
end
One more bit of is my routes
Rails.application.routes.draw do
root 'home#index'
get 'signup' => 'users#new'
resources :users
end
In Ruby you can reference a instance variable even if it has not been initialized:
irb(main):001:0> #foo
=> nil
Which is exactly what is happening here. If you want to use the same form on a bunch of actions without worrying if the controller sets the #user instance variable you can just use the || (or)
operator:
<%= form_for(#user || User.new) do |f| %>
# ...
<% end %>

Migration css rails login form to bootstrap

Need help migration to bootstrap.
bootstrap sound easy just go get all scss and samples to paste. But I don't know how to insert "#user, url: signup_path" into/or replace in bootstrap code to work in rails app.
html:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#user, url: signup_path) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Bootstrap code: from getbootstrap
<div class="input-group input-group-lg">
<span class="input-group-addon" id="sizing-addon1">#</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="sizing-addon1">
</div>
<div class="input-group">
<span class="input-group-addon" id="sizing-addon2">#</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="sizing-addon2">
</div>
<div class="input-group input-group-sm">
<span class="input-group-addon" id="sizing-addon3">#</span>
<input type="text" class="form-control" placeholder="Username" aria-describedby="sizing-addon3">
</div>
Updated:
bootstrap as follow:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages', object: #user %>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">#</span>
<%= f.submit yield(:button_text), class: "btn btn-primary" %>
</div>
<% end %>
Something like this:
<%= form_for(#user, url: signup_path) do |f| %>
<div class="input-group input-group-lg">
<span class="input-group-addon" id="sizing-addon1">#</span>
<%= f.input :name, input_html: {class: 'form-control'}, placeholder: 'Name' %>
</div>
<div class="input-group">
<span class="input-group-addon" id="sizing-addon2">#</span>
<%= f.input :email, input_html: {class: 'form-control'}, placeholder: 'Email' %>
</div>
<div class="input-group">
<span class="input-group-addon" id="sizing-addon3">#</span>
<%= f.password_field :password, input_html: {class: 'form-control'}, placeholder: 'Password' %>
</div>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
I hope you get the idea!

How to integrate Ruby with bootstrap search bar

I am trying to integrate Ruby code into the bootstrap search bar.
Here's my code thus far:
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
# Search function
<%= form_tag deals_path, :method => "get" do %>
<%= text_field_tag :search, params[:search], placeholder:"Search a deal name %>
<%= submit_tag "Search", class: "btn btn-default" %>
<%=form_tag deals_path,{:method=>'get', class:"navbar-form navbar-left", role:"search"} do%>
<div class="form-group">
<%=text_field_tag :search,"",class:"form-control",placeholder:"Search"%>
</div>
<%=submit_tag 'Seach' ,class:"btn btn-default "%>
<%end%>

Rails contact form alignments

So I have this contact form in rails
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description, :size=>"30x3" %>
</div> <br/>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
I want my form to utilize full page, i.e. I want the fields till phone to be on the left side and the description as a text area on the right horizontally.
Something like this
I am using bootstrap with the rails.
I tried with col-md-4 for 1 part and col-md-8 for another. doesn't fix that.
Please help.
From this template i made here:
http://www.bootply.com/XAMvtjjoQw
You can adapt it to ruby like this:
<form>
<div class="col-md-4">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<%= f.text_field :name, :class => "form-control" %>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<%= f.text_field :email, :class => "form-control" %>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<%= f.text_field :phone, :class => "form-control" %>
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<label for="exampleInputFile">File input</label>
<%= f.text_area :description, :class=>"form-control", :size=>"30x3" %>
</div>
</div>
</form>
for a pure HTML layout you can do this:
<form action='' method='post'>
<div class='row'>
<div class='col-md-2'>
<div class='control-group'>
<label for="name">Name</label>
<input id="name" type='text' name='name' value='' class='form-control'>
</div>
<div class='control-group'>
<label for="email">Email</label>
<input id="email" type='text' name='email' value='' class='form-control'>
</div>
<div class='control-group'>
<label for="phone">Phone</label>
<input id="phone" type='text' name='phone' value='' class='form-control'>
</div>
</div>
<div class='col-md-10'>
<div class='control-group'>
<label for="description">Description</label>
<textarea id="description" name='description' class='form-control' rows='4'></textarea>
</div>
<div class='control-group'>
<br><input type='submit' name='submit' value='Submit' class='btn btn-sm btn-info'>
</div>
</div>
</div>
</form>

Rails Bootstrap how to format form_for (width grid collapses)

I'm trying to make a form with bootstrap-sass. I need to find the optimal configuration of bootstrap classes to make it look nice. It is not showing up the way I expect, specifically when the browser width is smaller than a certain size the spaces between labels and form fields collapse and it doesn't look good anymore. It would be ok if it only happens on small width, but that's not the case.
I would really appreciate some help with this,
really, what is the best way to format the form-horizontal with bootstrap?
Here's my code:
<form class="form-horizontal center" role="form">
<%= form_for #user do |f| %>
<div class="field">
<%= f.label :fname, "First name:", class: "col-md-4 control-label" %>
<%= f.text_field :fname %>
</div>
<div class="field">
<%= f.label :lname, "Last name:", class: "col-md-4 control-label" %>
<%= f.text_field :lname %>
</div>
<div class="field">
<%= f.label :email, "Email:", class: "col-md-4 control-label" %>
<%= f.text_field :email %>
</div>
<!--div class="form-group" -->
<div class="field">
<%= f.label :comments, "Comments:", class: "col-md-4 control-label" %>
<%= f.text_field :comments %>
</div>
<div class="field">
<%= f.radio_button :attend, 'yes', :checked => true, class: "col-md-4 control-label" %>
<%= f.label :attend, 'I will attend.', :value => "yes" %><br />
<%= f.radio_button :attend, 'no', :checked => false, class: "col-md-4 control-label" %>
<%= f.label :attend, "I will not attend.", :value => "no" %>
</div>
<div class="field">
<%= f.check_box :workshop, class: "col-md-4 control-label" %>
<%= f.label :workshop, "Checkbox Description" %>
</div>
<div class="field">
<div class="col-md-4">
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
</div>
</div>
<% end %>
</form>
As you can see from commented out code, I first used form-group class, but it wasn't working well.
Again, the problem is that spaces between labels and text fields collapse when width of the browser is less than certain size. The labels, which are right-aligned, lose their alignment. Browser has to be almost full screen to show up correctly, which isn't right because there's plenty of space for it to show up correctly on smaller width.
I was following this guide http://getbootstrap.com/css/#grid
Edit: Added email field in the code, because it's of different size and easier to see the problem.
take a look at bootstrap v3 doc here http://getbootstrap.com/css/#forms-horizontal
and you don't need to add form tag since you are already using form_for
<%= form_for #user, :html => {:class => "form-horizontal center"} do |f| %>
<div class="form-group">
<%= f.label :fname, "First name:", class: "col-md-4 control-label" %>
<div class="col-md-8">
<%= f.text_field :fname, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :lname, "Last name:", class: "col-md-4 control-label" %>
<div class="col-md-8">
<%= f.text_field :lname, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :comments, "Comments:", class: "col-md-4 control-label" %>
<div class="col-md-8">
<%= f.text_field :comments, class: "form-control" %>
</div>
</div>
<div class="radio">
<%= f.radio_button :attend, 'yes', :checked => true %> I will attend.
<br />
<%= f.radio_button :attend, 'no', :checked => false %> I will not attend.
</div>
<div class="checkbox">
<%= f.check_box :workshop %> Checkbox Description
</div>
<%= f.submit "Submit", class: "btn btn-default btn-primary" %>
<% end %>

Resources