Add a link to a label/hint in formtastic with haml - ruby-on-rails

I'm trying to add a link to a checkbox in formtastic, using haml. It looks like formtastic removes all ruby/haml/html syntax from the text for the label or hint when it parses it.
That is, this doesn't work:
= f.input :terms, label: '=link_to \'Here\', path_to_link', hint: '=link_to \'Other Place\', path_to_other_link', as: :boolean
Is there anything that will other than writing another div outside of this input? Or am I going about this all wrong? I'm a rails/haml/formtastic noob.

if you use ' for string quotation, you're basically telling ruby not to parse that content.
try something like this instead:
= f.input :terms, label: link_to('Here', path_to_link), hint: link_to('Other place', path_to_other_link), as: :boolean

Formtastic::FormBuilder.escape_html_entities_in_hints_and_labels = false
In your formtastic initializer

If you mark the string passed to label: or hint: as HTML safe then Haml doesn't escape it.
= f.input :terms,
label: "Please accept our ".html_safe + link_to('Terms and Conditions', path_to_link)

19 ways tried, with either the hyperlink being encoded or html_safe replacing hyphens in the url ???
This is what worked for me
<%= f.label :cookies do
"Do you agree to our #{link_to('Cookies Policy', 'http://www.your-­url.co.uk/privacypolicy')}".html_safe
end %>
The specific use of " and ' appears significant.

Related

Rails Form Select Requirement with partial

I'm creating a form with a .select field that loads a list of states via partial. The requirement isn't being enforced on state and I'm not sure why. It lets you submit the form with the default blank value 'State'
Would appreciate any help figuring out where my syntax is wrong on this form? If this looks foreign, using SLIM instead of HTML.
= f.select :state, nil, include_blank: 'State', required: true # not working
= render partial: 'addresses/states'
= f.text_field :zip, placeholder: 'Zip', required: true, pattern:'[0-9]*' # works
The states partial looks like this:
option value="AL" AL
option value="AK" AK
option value="AZ" AZ
option value="AR" AR
...

How to override text field to textarea in rails_admin

First time I try to customize rails_admin and by default I am getting text field which I want to convert in textarea. In model I have given datatype as string So is that possible to display textarea?
config/initializer/rails_admin.rb
config.model Product do
list do
exclude_fields :id, :created_at, :updated_at
end
create do
......
configure :description do
partial 'my_partial_file' # to override field I have created partial file
end
.....
end
end
views/rails_admin/main/_my_partial_file.html.haml
= form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
I tried html_attributes rows: 50, cols: 60 also tried to apply custom_css but doesn't help. Please guide me where I do mistake? And if possible please make me understand syntax of this _my_partial_file
Edit:
If I do something like this then I can get textarea
field :description, :text do # use second parameter to set field type
required true
#partial 'my_partial_file'
end
But if I render partial then again text_field shown. :( I want text_area + partial file should also rendered as it contain other code to process.
I have solved this issue by my own. I thought to delete this question but then realized if in future anyone faced same issue then my solution can be helped.
What I had changed in _my_partial_file is:
= form.text_area field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
........ # other piece of code
and in config/initializer/rails_admin.rb I have keep the code as it is:
create do
......
configure :description do
partial 'my_partial_file'
end
.....
end
I got the reference from here: https://www.omniref.com/ruby/gems/obitum-rails_admin/0.0.5/files/app/views/rails_admin/main/_form_text.html.haml#line=5 (Wayback link)
and
http://ruby-doc.org/gems/docs/r/rails_admin_settings-0.8.0/app/views/rails_admin/main/_setting_value_html_haml.html
(Note: link dead. This may be the same file from that older version: https://github.com/rails-admin/rails_admin_settings/blob/v0.8.0/app/views/rails_admin/main/_setting_value.html.haml )

Suppress square brackets in input names generated by simple_form

I'm working on a bookmarkable search form - with simple_form in order to get access to some custom inputs. Since no real model object is connected, I use the :q symbol to fake one:
= simple_form_for :q, url: projects_path, method: :get do |f|
= f.input :area_id,
as: :select,
collection: (...)
= f.input :description,
as: :geocomplete
While this works, the naming conventions produce in not so nice URLs such as:
...?q[area_id]=16&q[description]=Paris&q[lng]=4.123&q[lat]=30.123
Is there a way to tell simple_form to suppress the fake :q object and produce URLs like:
...?area_id=16&description=Paris&lng=4.123&lat=30.123
Thanks for your hints!
Like #Martin wrote, it can't be done. So it's either standard form helpers or accept the square brackets. I've chosen the latter.

How to escape colon : in rails i18n?

Here is a line in the view:
<%= f.input :comm_date, :label => "Hello:", :as => :string %>
The regular i18n file has the format as:
Hello : 'Aloha'
In i18n yml file, we want to translate the "Hello:" as, for example, "Aloha:". Can we escape the colon : by doing below?
'Hello:' : 'Aloha:'
Or what's the right way to escape a colon?
The gem you are using, simple_form, as well as other similar ones such as formtastic, allow for using translation files (e.g. en.yml) for defining your label text.
You can see the translation file expectations from the simple_form documentation. Using your example, you'd need two translations, something like this, assuming your model is called Communication:
en.yml
en:
simple_form:
labels:
communication:
comm_date: "Hello:"
olelo.yml
olelo:
simple_form:
labels:
communication:
comm_date: "Aloha:"
And your view would simply be
<%= f.input :comm_date, :as => :string %>
No need to provide a value for the :label option.
Here is what we did:
'Hello:' : 'Aloah'
Just use single quote and it worked.

Rails: How to disable asterisk on form's required fields?

When I add the 'Required' attribute
to html input fields, Rails pre-pends an asterisk (*) before the label.
Does anyone know how to prevent this?
For some reason Rails transforms this:
<%= f.input :Company, :input_html => {:value => "", :id => "company_name"}, :label => "company name" %>
into this:
<div class="input string required">
<label for="company_name" class="string required">
<abbr title="required">*</abbr> company name</label>
<input type="text" value="" size="50" required="required" name="lead[Company]" id="company_name" class="string required">
</div>
I don't like that it wraps everything in a DIV and adds an ABBR element to the party.
How can I prevent this?
You can just set the required mark to empty value in simple_form's locale file:
en:
simple_form:
required:
text: 'required'
mark: '*'
Or use CSS to hide it.
In config/initializers/simple_form.rb add this line:
config.label_text = lambda { |label, required| "#{label}" }
I'm using Rails 3.1, and I have the following view code in my _form.html.erb for a given model:
<div>
<%= f.label :full_name %><br/>
<%= f.text_field :full_name, :required => true %><br/>
</div>
The label does not show an asterisk if you do it this way. Unless you post code I can't be sure of what your approach is and if my solution would fit said approach.
Updated Answer:
It sounds like you've inherited this code from someone. At any rate, after reading your code sample, you are most definitely using the simple_form gem. Information about that gem can be found here https://github.com/plataformatec/simple_form. To answer your question though, if you change your code the following:
<%= f.input :Company, :input_html => {:value => "", :id => "company_name"}, :label => "company name", :required => false %>
That should turn off the asterisk.
I would add, based on your disgust for the HTML generated from simple_form, it sounds like you should just do away with the gem and re-write your form code using the Rails default form helpers, which can be read about here http://guides.rubyonrails.org/form_helpers.html. Depending on the size of the code base, you might be better off just sucking it up and learning how to use the simple_form gem for the sake of saving time, but if you think you have the time to change it all, go for it.
The simplest way is to hide it with this css:
abbr[title="required"] {
display: none;
}
It isn't rails at all. It's the simple_form gem. So, if you don't want all the wrapping elements don't use simple_form. Use Rails form helpers. It would be more simple than customize something you don't like.
For anyone using Formtastic and having this issue, you can remove the asterisks by editing the config file, which is typically app/config/initializers/formtastic.rb.
Change this line: # Formtastic::SemanticFormBuilder.required_string = "(required)"
to be: Formtastic::SemanticFormBuilder.required_string = ""
More info here.
Code that has helped me solve the asterisk issue:
abbr[title="required"] {
display: none;
}
The chosen answer and the other suggestions asking to change the HTML in locales file dint help me with the latest Simple_form gem.
Aside from the global config suggested in the accepted answer, you can pass required: false as an input option, or defaults: { required: false } to set it for the whole form.
You can remove it from the whole form:
<%= simple_form_for #form, defaults: { required: false } do |f| %>
I found out that if you only want to remove the asterisk(*) behind it then all you have to do is to go to this file file /config/locales/simple_form.en.yml
once again is not a good practice to change your configuration files for gems and something your using for some reason, it always a question of why do you really use simple_form!
But for example I found out about that because there is great things about simple_form we use but nowadays is a better usability practice to have the asterisks on none required fields then required ones.
you can used form_for, and override method def label in config/initializer to add asterisk for mandatory fields as the following:
def label(object_name, method, content_or_options = nil, options = nil, &block)
if content_or_options.is_a?(Hash)
content_or_options.each do |key, val|
options[key] = val
end
content_or_options = method.to_s
end
content_or_options ||= method.to_s
presence_validations = [ActiveModel::Validations::PresenceValidator, ActiveRecord::Validations::PresenceValidator]
class_obj = options[:object].class if options[:object]
class_obj ||= object_name.to_s.camelize.constantize
validations = class_obj.validators_on(method.to_s).map(&:class)
if (presence_validations.map { |pv| validations.include?(pv) }).any?
content_or_options += "*"
end
Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
end
This method puts asterisk after all mandatory fields, if you used normal form_for, and used validates_presence_of

Resources