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
Related
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'
I Have a code in my project like
<div class="form-inputs">
<%= f.input :twitter %>
</div>
It gives label as Twitter in my website. How it produce label with out giving label parameter. I want to change label to something else like Tweet it.
You can add label in very simple way
<%= f.input :twitter, label: 'Tweet it' %>
Check this simple_form usage
If you want to disable label
<%= f.input :input_field_name, label: false %>
Add custom class to label
<%= f.input :field_name, label_html: { class: 'my_class' } %>
This question already has answers here:
Rails: How to disable asterisk on form's required fields?
(11 answers)
Closed 5 years ago.
i am using the simple form gem and the inputs labels shows an Asterisk " * " before text and i do not need that
something like:
*Year
*Month
*Week
*Quantity
simple form view:
<%= simple_form_for #tech_evaluation do |f| %>
<%= f.input :year, label: "Año", collection: 2017..2020 %>
<%= f.association :project, label: "Proyecto" %>
<%= f.association :countable_account, label: "Cuenta contable" %>
<%= f.association :item %>
<%= f.input :unit_cost, label: "Costo unidario" %>
<%= f.input :unit, label: "Unidad de medida", collection: [ "C/U" ] %>
This is a possible duplicate of How to disable asterisk on form's required fields?
If you want to disable asterisks for that form only, pass defaults: { required: false } into simple_form_for:
<%= simple_form_for(#tech_evaluation, defaults: { required: false }) do |f| %>
# ...
Or you can disable it even for just a single input element:
<%= f.input :year, label: "Año", collection: 2017..2020, required: false %>
Read more at Simple Form: Usage
Lastly, if you want to disable the asterisk mark for ALL simple forms, you can do so by creating the file simple_form.en.yml under config/locales/ with this content:
# config/locales/simple_form.en.yml
en:
simple_form:
required:
text: 'required'
mark: '*'
# This will overwrite required text and mark for all simple forms.
# When using html, text and mark won't be used.
# Comment out the line below to return to default settings.
html: ''
See more configuration options at simple_form.en.yml on Gituhb
I'm trying to figure out how to use simple form without the pre-determined label.
I've tried setting label to false as set out below, but it doesn't stop the label of the attribute ('trl') from appearing beside the collection.
<%= f.input :trl do %>
<%= f.select :trl, Trl.all.map { |t| [t.title, t.id] }, label: false, include_blank: false, prompt: 'Select one' %>
<% end %>
Is there a way to dis-apply the labels in simple form?
try this:
<%= f.input :trl, label: false do %>
<%= f.select :trl, Trl.all.map { |t| [t.title, t.id] },include_blank: false, prompt: 'Select one' %>
<% end %>
I have a enum definition for my User model.
class User < ActiveRecord::Base
enum program_of_study: [
:program_of_study_unknown, :program_of_study_cs, :program_of_study_ceg,
:program_of_study_is, :program_of_study_science,
:program_of_study_engineering, :program_of_study_fass,
:program_of_study_business, :program_of_study_others
]
end
And in simple_form I have:
<%= simple_form_for(locals[:user], wrapper: :horizontal_form, html: {class: 'form-horizontal'},
url: {action: (locals[:is_new] ? 'create' : 'update')}) do |f| %>
<%= f.error_notification %>
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: locals[:user].program_of_study %>
<%= f.button :submit, class: 'btn-success' %>
<% end %>
However, it seems that although the program_of_study of a user is 'program_of_study_science'(by checking in rails console), when I render the form, the shown select element still has 'program_of_study_unknown' as the displayed one. The correct one was not selected.
Instead of the enum I used the keys and it seems to work, have you tried that:
collection: User.program_of_studies.keys
I didn't have to specify a selected option. My code looks like this:
input :status, as: :select, collection: Venue.statuses.keys, include_blank: false
My solution in the end
<%= f.input :program_of_study, collection: User.program_of_studies, include_blank: false, selected: User.program_of_studies[locals[:user].program_of_study] %>