I'm building a rails application and want to add a time tracker. The users should be able to log a time (I worked for 3 hours) and submit, or they should be able to start a timer, wait, and then submit. I picked VueJS to build it out, and successfully have integrated Vue with my Rails 5 application.
I built out a simple timer (start, stop, display current time) within Vue and embedded in my page. Now I want the user to fill out some additional fields and save the time entry.
I've attempted to just take the values for the hours, minutes, and seconds from the Vue data and connect it using v-models to a basic simple_form time field, but since rails automagically generates the input fields as time_entry_duration_4i and time_entry_duration_5i, I'm struggling to connect the data.
Should I even go about pushing the JS data into a rails form? Should I just rebuild the form so it's all in Vue and always submitted that way?
Thanks.
#time_entries/_form.html.haml
= simple_form_for(#time_entry) do |f|
= f.error_notification
= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?
.form-inputs.row
.col
= f.association :project, collection: Project.joins(:assigned_roles).where(assigned_roles: {team_member: current_user}).distinct, hint: "If this is blank, you're not currently assigned to any projects."
= f.input :task, collection: TaskType::TASK_TYPES.sort {|a, b| a <=> b }
.form-inputs.row
// This is the Rails way of doing things and works, but I can't connect it to the v-model
= f.input :duration, as: :time, wrapper_html: { class: 'col-6'}, default: Time.parse('0:00')
.col-6.form-group
.d-flex.flex-row.justify-content-between
= f.label :duration, label: "Hours"
= f.label :duration, label: "Minutes"
= f.label :duration, label: "Seconds"
.d-flex.flex-row.justify-content-between.align-items-center
// This is my attempt to recreate the hours/min/seconds and bind to the v-model, but it doesn't submit
= f.text_field :duration, "v-model" => "hours", class: 'form-control mx-1'
= f.text_field :duration, "v-model" => "minutes", class: 'form-control mx-1'
= f.text_field :duration, "v-model" => "seconds", class: 'form-control mx-1'
= f.input :date, wrapper_html: { class: 'col-6'}
.form-inputs.row
.col
= f.input :notes, placeholder: "What were you working on?"
.form-actions
= f.button :submit, class: 'btn btn-primary btn-block'
You can mix rails and Vue by writing the inputs with plain html:
.col-6.form-group
.d-flex.flex-row.justify-content-between
= f.label :duration, label: "Hours"
= f.label :duration, label: "Minutes"
= f.label :duration, label: "Seconds"
.d-flex.flex-row.justify-content-between.align-items-center
input type="text" name='time_entry[duration(4i)]' v-model="hours" class='form-control mx-1'
input type="text" name='time_entry[duration(5i)]' v-model="minutes" class='form-control mx-1'
input type="text" name='time_entry[duration(6i)]' v-model="seconds" class='form-control mx-1'
Related
I have a time field which stores an integer(minutes). I want the user to be able to enter in days, hours and minutes in the form rather than entering minutes.
.form-group
= f.label :time, class: "col-sm-2 control-label"
.col-sm-10
= f.text_field :time, class: "form-control"
Is there an easy way to do this via the form so that I would have...?
.form-group
= f.label :days, class: "col-sm-2 control-label"
.col-sm-10
= f.text_field :days, class: "form-control"
.form-group
= f.label :hours, class: "col-sm-2 control-label"
.col-sm-10
= f.text_field :hours, class: "form-control"
.form-group
= f.label :minutes, class: "col-sm-2 control-label"
.col-sm-10
= f.text_field :minutes, class: "form-control"
I realise I could just have days,hours and minutes as database fields but I'd rather just keep my single time field.
Came across a similar situation, this is what helped me.
gem time_splitter
Looks like this will accomplish the job for you as well, simply runs on top of whatever datetime fields you have for your model.
You could hide the time field and have your days, hours and minutes field showing and use javascript to calculate the time in minutes based on the values provided on the fields that are showing and just submit that.
so it would be something like (and this is pseudo code):
time.value = (days.value * 1440) + (hours.value * 60) + minutes.value.
I have a simple_form that has a bunch of urls that need to be inputted into the database.
Now i'm wanting to have a new record matches existing record page where you can compare the issues?
For instance. If a record has google.com/search/stackoverflow
And if someone types in the same url to be inputted into the same column name. It will alert the user stating that the field already exists and display the ID number of the field that exists. It then wont save the new field and will redirect the user back to the dashboard.
Heres the code i have at the moment.
Controller:
def create
create_params = params[:newevent].permit(:eventname, :eventshortdesc, :eventvenuename, :eventdesc, :eventdatetime, :eventimage, :1link, :2link, :3link, :4link, :5link, :6link, :event_type, :eventready, :eventcomplete)
#newevent = Newevent.new(create_params)
#newevent.save!
end
view
<%= f.input :eventname, label: "Event Name", required: true %>
<%= f.input :event_type %>
<%= f.input :eventdesc, label: "Description", required: true %>
<%= f.input :eventshortdesc, label: "Short Description", required: true %>
<%= f.input :eventvenuename, label: "Venue Name / Location", required: true %>
<%= f.input :eventdatetime, type: "datetime", label: "Date Of Event: (Enter as YYYY-MM-DD)", required: true %>
<%= f.input :eventimage, :label => "Image Name Here (exact)"%>
<%= f.input :1link, label: "1 URL of Event" %>
<%= f.input :2link, label: "2 URL of Event" %>
<%= f.input :3link, label: "3 URL of Event" %>
<%= f.input :4link, label: "4 URL of Event" %>
<%= f.input :5link, label: "5 URL of Event" %>
<%= f.input :6link, label: "6 URL of Event" %>
This is all i have at the moment.
Thanks for helping if you can.
Sam
You just need a simple uniqueness validation.
class NewEvent < ActiveRecord::Base
validate_uniqueness_of :1link, :2link, :3link # whatever attribute you want to validate
end
I have problem with showing an error to date field with simple_form.
I have the following code in my registration form:
.row
.col-md-6.col-md-offset-3
%fieldset
%h2 Rejestracja
%hr.colorgraph/
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
= f.error_notification
.form-group
= f.input :username
.form-group
= f.label "Birthdate"
%br
= f.date_select :birth_date, end_year: 1950, start_year: (Time.now.year - 18)
.form-group
= f.input :email
.form-group
= f.input :password
.form-group
= f.input :password_confirmation, :required => false
.row.center
.col-xs-6.col-sm-6.col-md-6.center
= f.button :submit, "Submit", class: "btn btn-lg btn-success btn-block"
And the following validation of age in my user model:
validate :at_least_18
def at_least_18
if self.birth_date
errors.add(:birth_date, 'You must be 18 years or older.') if self.birth_date > 18.years.ago.to_date
end
end
The problem is that when i submit the form with valid birth_date, simple_form does not show the error like on the other fields:
But when i change
= f.date_select :birth_date, end_year: 1950, start_year: (Time.now.year - 18) to
= f.input :birth_date
It is working but now i have three ugly fields...
Any ideas?
This is caused by the CSS styling of inputs with errors.
You could fix this by adding some CSS to correctly style the dropdowns, however I think that fundamentally you would be better off replacing the 3 dropdown boxes with a single text field and using something like jquery's date picker to populate it.
Not only will that give your users a far better date-inputting experience, but your error layout issue will also be fixed.
As always with Ruby on Rails I use simple_form gem to handle forms in more elegant and, well, simple way. But i have this problem, that string inputs are not generated properly. i mean they work as they are supposed to, but they are just extremely long, like totally out of my screen. it isn't normal behaviour is it? should i apply any special css for this? or maybe there is some other solution?
This form i have looks like this:
= simple_form_for #book, url: books_path, method: :post do |f|
= f.input :title, label: false, placeholder: "title"
= f.input :author, label: false, placeholder: "author"
= f.input :isbn, label: false, placeholder: "ISBN"
= f.input :publishing_date, label: false, placeholder: "Publishing date"
= f.input :amount, label: false, placeholder: "Amount"
= f.submit class: "btn btn-success"
Other that this i just put 'gem "simple_form"' to the Gemfile and thats it.
There is a good answer I found from typing simple_form width:
{:maxlength =>2,:style=> 'width: 20px'}%>
from this answer:
Rails Simpleform setting the width of a form input element
tell me how to configure simple_form. I would like to use the checkbox of semantic ui, but when I wrap checkbox in the class, he becomes active, that is, visually it is, but when you click the check box is not activated.
= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f|
.ui.three.column.middle.aligned.relaxed.grid.basic.segment
.column
.column
.column
.ui.form.segment
#marg.field= f.label :email, 'Email'
#marg.field=f.input :email, placeholder: 'Email', autofocus: true, label: false
#marg.field= f.label :password, 'Пароль'
= f.input :password, :required => false, placeholder: 'Пароль', label: false
#marg.ui.toggle.checkbox
= f.input :remember_me, as: :boolean if devise_mapping.rememberable?
#marg= f.button :submit, 'Войти!', class: 'small ui blue submit button'
http://i.imgur.com/C8Wn4K9.png
Please try it
= f.input :remember_me, as: :boolean, boolean_style: :inline
A simpler way to do this is to create a custom simple form wrapper. Here is a blogpost which describes how to add the wrapper: http://pranavsingh.me/semantic-ui-simple-form-wrapper/
The above configuration will automatically add all the essential classes semantic form classes, and also adds wrappers to create proper checkbox fields. Below is an example:
= f.input :published, label: "Published?",
hint: "If you are not ready to go live yet, keep this site unpublished.",
wrapper: :ui_toggle_checkbox
Here's what I had to do (the accepted answer got me on the right track, but didn't fully fix the issue):
In the config/initializers/simple_form.rb file, change
config.boolean_style = :nested
to
config.boolean_style = :inline
This prevents Simple Form from wrapping the input tag in the label tag (essentially inserting a tag between the '.ui .checkbox' div and the input, which breaks Semantic's checkbox functionality).
In the app/assets/javascripts/application.js file:
$('.ui.checkbox').find('input').checkbox();
My form now displays checkboxes correctly and passes the appropriate value when submitted.
Here is another option (to just fix one checkbox) -
In a _form.html.erb partial:
<div class="inline field">
<div class="ui checkbox">
<%= my_form.input :my_input, label: 'My Label', as: :boolean, boolean_style: :inline %>
</div>
</div>
In the application.js file:
$('.ui.checkbox').find('input').checkbox();