I'm using the gem simple_form and trying to do a ".mb_chars" on my field. Any idea?
My field:
<%= f.input :metadesc_mb, label: 'Meta description' %>
Maybe something like this (don't work):
<%= f.input :metadesc_mb.mb_chars, label: 'Meta description' %>
Thanks in advance.
I believe you should use :value_method key to pass the string processed with #mb_chars. That will
something like the following:
<%= f.input :metadesc_mb, label: 'Meta description', value_method: -> { self.metadesc_mb.mb_chars } -%>
or to use :value subkey of :input_html key:
<%= f.input :metadesc_mb, label: 'Meta description', input_html: { value: self.metadesc_mb.mb_chars } -%>
NOTE: The last method can't be applied to collections.
Related
I am trying to remove the label that is auto included when we use '.association' of the simple_form_for. But, regardless of what I do, the title and its <hr> continue to be displayed.
I tried:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: false %>
and
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label: "" %>
But it keeps showing :/
What can I do to remove it?
You can try this:
<%= f.association :attr_vals, collection: attr.attr_vals,
as: :check_boxes, wrapper: false, label_method: "" %>
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] %>
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'm using simple form, and I have the :selected => 0 so that the select has value 0 from its collection by default. But this is not working, the select box is blank and I need to choose the "0" option manually... I'm in a modal by the way. Any idea?
<%= f.input :price_type, label: "¿Conoces el precio?", collection: ["0","1","2"], :selected => 0 , id: "tag_price_type", html_input: "required", input_html: {class: 'ipt'} %>
Try matching the object types between the collection and the selection. So either:
<%= f.input :price_type, label: "¿Conoces el precio?", collection: [0,1,2], selected: 0 , id: "tag_price_type", html_input: "required", input_html: {class: 'ipt'} %>
or
<%= f.input :price_type, label: "¿Conoces el precio?", collection: ['0','1','2'], selected: '0' , id: "tag_price_type", html_input: "required", input_html: {class: 'ipt'} %>
You have to use quotes, see: How do I set the default selected item in a Rails drop-down menu?
So you should have: :selected => "0"
I'm trying to set a label for the following line but I keep getting an error if I add label_method anywhere. label: is just ignored. How can I add a label for f.select?
<%= f.select :state_identifier, Location::STATE, { prompt: 'State', id: 'state' } %>
I tried the following but it doesn't format properly in form-horizontal, leaving no gap between the label and data.
<%= f.label :state_identifier, label: 'State' %>
This is because f.select is not a simple_form method and does not support :label
Something like this should work for you w/ simple form.
<%= f.input :state_identifier, :label => "State", :collection => ["a","b"], :input_html => {:id=>"state" } %>
Hope this helps.