Converting Trent Richardson's DateTime Picker output to Rails-friendly format - ruby-on-rails-3

I'm having trouble getting Trent Richardson's DateTime picker to format output in a way that Rails can understand and properly save to the database.
The following is my model:
class Reservation < ActiveRecord::Base
attr_accessible :email, :message, :booking_time, :restaurant_id
belongs_to :restaurant
validates :booking_time, :presence => true
validates :email, :presence => true
end
The following is in my application.js file:
$(function () {
$( "#bookingTime" ).datetimepicker({
timeFormat: "hh:mm tt",
stepMinute: 5,
hourMin: 8,
hourMax: 22
});
});
The following is my _form.html.erb file:
<div class="control-group">
<%= f.label :booking_time, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :booking_time, :id => 'bookingTime', :"data-required" => "true", :class => "required", :"required" => "true" %>
</div>
</div>
I'm new to Rails and can't determine how to format the time and date in a way that Rails can properly save to the database.
Any suggestions greatly appreciated.
Thanks!

Related

Simple form & Cocoon nesting form to select from an associated attribute

Nesting forms always seems to mangle my brain. Any help would be appreciated.
I want to make a booking form for a course. The course has start dates associated with it. I want the form to update the course start dates once the course is selected from the dropdown selection in the form. I have this much done. I've added cocoon gem and have the forms all set up but I cannot seem to get the form to generate a list based on the course selected in the form.
Models:
class Booking < ApplicationRecord
belongs_to :course
belongs_to :user
end
class Course < ApplicationRecord
has_many :bookings
has_many :schedules
accepts_nested_attributes_for :schedules, :reject_if => :all_blank, :allow_destroy => true
end
class Schedule < ApplicationRecord
belongs_to :course
end
Form Views
Booking Form
<%= simple_form_for(#booking) do |f| %>
<%= f.error_notification %>
<div class="ice-card">
<!-- <div class="form-inputs">-->
<%= f.input :user_id, :input_html => { :value => current_user.id} %>
<%= f.input :user_email, :input_html => { :value => current_user.email}, label: "Contact Email" %>
<%= f.association :course, label_method: :header, :input_html => { :selected => :course }, :include_blank => false, label: "Course Name" %>
<%= f.simple_fields_for :schedules do |schedule| %>
<%= render 'schedule_fields', :f => schedule %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit, class: "btn btn-primary" %>
</div>
<% end %>
Schedule Form partial
<%= f.input :start_date, as: :date %>

How to resolve blank passowrd field issue in ROR

Can anybody resolve my one issue.I want to save all of my values in sqlite database.I have name,emailid and password field.when i clicked on submit button only name and email-id has stored in database bit i found that password filed is showing blank.I am using rails version-4.0.2 and ruby 1.9.3.The codes are given below.
views/users/index.html.erb:
<h1>This is index page</h1>
<center>
<p>Enter data</p>
<div class="option">
<p><%= link_to "Click here to enter data",users_new_path %></p>
<p><%= link_to "Display data",users_display_path%></p>
</div>
</center>
views/users/new.html.erb:
<h1>Enter your data here</h1>
<center>
<%= form_for #user ,:url => {:action => "create"} do |f| %>
<div class="div_reg">
<p>
<label for="username" class="uname" data-icon="u" >username </label>
<%= f.text_field:name,placeholder:"Enter your user name" %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Email </label>
<%= f.text_field:email,placeholder:"enter your email" %>
</p>
<p>
<label for="username" class="uname" data-icon="u" >Password </label>
<%= f.password_field:password,placeholder:"Enteryour password" %>
</p>
<center>
<%= f.submit "Submit",:class => 'btn-custom' %>
</center>
<div class="back_btn">
<button type="button" class="btn-custom " style="cursor:pointer;">Back</button>
</div>
</div>
<% end %>
</center>
<% if #user.errors.any? %>
<ul class="Signup_Errors">
<% for message_error in #user.errors.full_messages %>
<li><%= message_error %></li>
<% end %>
</ul>
<% end %>
controller/users_controller.rb:
class UsersController < ApplicationController
def index
end
def new
#user=User.new
end
def create
#user=User.new(users_param);
if #user.save
flash[:notice]="You signed up successfully"
flash[:color]="valid"
redirect_to :action => 'index'
else
flash[:alert]="You have not signed up successfully"
flash[:color]="invalid"
redirect_to :action => 'new'
end
end
private
def users_param
params.require(:user).permit(:name, :email, :password)
end
def display
end
end
model/user.rb:
class User < ActiveRecord::Base
attr_accessor :password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :name, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
end
migrate\20150102052336_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.timestamps null: false
end
end
end
Please help me to resolve this issue.Thanks in advance.
You should add a password_confirmation field
<%= f.password_field :password %>
<%= f.password_field> :password_confirmation %>
add also add it into the permitted parameters
params.require(:user).permit(:name, :email, :password, :password_confirmation)
PS: I recommend looking up devise gem, once you figured how to create your own signup/login implementation..

simple_form and Twitter Bootstrap and input-append code

I'm trying to use the input-append feature of Twitter Bootstrap. However, I want to try and keep my code cleaner with the simple_form.
<div class="control-group string required">
<label class="string required control-label" for="video_company_id"><abbr title="required">*</abbr> Company</label>
<div class="controls">
<div class="input-append">
<%= f.text_field :company_id, :class => "span2" %>
<span class="add-on"><%= link_to image_tag('/assets/button/magnifier.png'), "#select_company", :"data-toggle" => "modal" %></span>
</div>
</div>
</div>
yields
What is a better way to write this with simple_form using f.input?
Edit to mdepolli
Thanks mdepolli. That does get me closer to where I need to be. However, it just puts the lookup inline with the form instead of an 'add-on'.
<%= f.input :company_id, :as => :string, :class => "span2", :wrapper_html => { :class => "input-append"} %>
<%= link_to image_tag('/assets/button/magnifier.png'), "#select_company", :"data-toggle" => "modal", :class => "add-on", :wrapper_html => { :class => "input-append"} %>
edit again
with the content_tag, it takes it to the separate line
First off, let's create a custom wrapper in the SimpleForm initializer.
In config/initializers/simple_form.rb:
config.wrappers :append, :tag => 'div', :class => "control-group", :error_class => 'error' do |b|
b.use :html5
b.use :placeholder
b.use :label
b.wrapper :tag => 'div', :class => 'controls' do |input|
input.wrapper :tag => 'div', :class => 'input-append' do |append|
append.use :input
end
input.use :hint, :wrap_with => { :tag => 'span', :class => 'help-block' }
input.use :error, :wrap_with => { :tag => 'span', :class => 'help-inline' }
end
end
Now let's go back to your form.
Be sure to replace #model with the actual instance variable attached to the ActiveRecord model you're using.
<%= simple_form_for #model do |f| %>
<%= f.input :company_id, :wrapper => :append do %>
<%= f.input_field :company_id %>
<%= content_tag :span, link_to(image_tag('/assets/button/magnifier.png'), "#select_company", :"data-toggle" => "modal"), :class => 'add-on' %>
<% end %>
<%= f.button :submit %>
<% end %>
Edit: Sorry it took me a bit, ended up having to make a custom wrapper in the end.

Nested fields not being created on form

I am trying to get a Project form to build the first (starting) time of several (up to 12) volunteer time blocks.
project.rb
class Project < ActiveRecord::Base
attr_accessible :title, ...
has_many :vol_times, :dependent => destroy
accepts_nested_attributes_for :vol_times, :reject_if => lambda { |a| a[:start_time].blank? }, :allow_destroy => true
...
end
vol_time.rb
class Vol_time < ActiveRecord::Base
attr_accessible :start_time, ...
belongs_to :project
end
ProjectsController
class ProjectsController < ApplicationController
before_filter :signed_in_user, only: :create
...
def new
#project = Project.new
#user = current_user
#project.vol_times.build
end
...
end
Vol_Times Controller
class Vol_TimesController < ApplicationController
def new
#reward = Reward.new
end
...
end
My view looks like this...
<%= form_for(#project) do |f| %>
<div class="form_field_block">
<p class="form_label"> Project Title</p>
<%= f.text_field :title, :size => 40, :placeholder => " Project Title...", :class => "frm" %>
</div>
<div class="form_field_block">
<p class="form_label"> Project Sub-title</p>
<%= f.text_field :sub_title, :size => 40, :placeholder => " Project Sub-title...", :class => "frm" %>
</div>
<p class="clearing"></p>
<div class="form_field_block">
<% f.fields_for :vol_times do |builder| %>
<%= render :partial => 'start_time', :f => builder %>
<% end %>
</div>
<p class="clearing"></p>
<%= button_tag "btn_start_project.png", :class => "btn_save" %>
<% end %>
And the _partial looks like this...
<%= f.label :start_time, "Starting Time" %>
<%= f.text_field :start_time %>
When I view the page, I see the containing <div>, but not the contents of the ERB, which should be parsed from the _partial.
Any ideas why this isn't working? I got the general context from Ryan Bates' RailsCast #196 - Here
you are missing a = on the fields_for. It should be
<%= f.fields_for :vol_times do |builder| %>

Updating unused field after initial creation does not work rails

For reference, this is using Rails 3.0.9.
I can't seem to figure out why, but I cannot update a field which was left blank at the initial creation of the object/row.
If my model looks like this:
name - string
date - date
message - string
If I put info in all of the fields upon initial creation, everything can be updated without a hitch later on. But if I do not put info in one of the fields, say the title, I cannot update that field after initial creation.
I'm not doing anything out of the ordinary, the form is pretty plain jane:
<%= form_for #event do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :date %><br />
<%= f.date_select :date %>
</p>
<p>
<%= f.label :message %><br />
<%= f.text_field :message %>
</p>
<p><%= f.submit %></p>
<% end %>
And my controller is equally as simple:
def update
if #event.update_attributes(params[:event])
redirect_to #event, :notice => "Successfully updated event."
else
render :action => 'edit'
end
end
And the model:
class Event < ActiveRecord::Base
has_many :races, :dependent => :destroy
has_many :price_groups, :dependent => :destroy
accepts_nested_attributes_for :races, :reject_if => lambda{ |a| a[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :price_groups, :reject_if => lambda{ |a| a[:name].blank? }, :allow_destroy => true
attr_accessible :name, :date, :message, :races_attributes, :price_groups_attributes
end