Adding placeholder text to chosen chosen-jquery - ruby-on-rails-3

I am using the chosen-rails gem with great success. However, I want to customise the placeholder text. From the gem documentation it looks like the following should be in the coffee script file
$('.chzn-select').chosen
allow_single_deselect: true
no_results_text: 'No results matched'
When I use this code I can alter the deslectbehaviour, so the script is being called, but the placeholder text does not change
has anyone experienced similar behaviour?

You can just set your data-placeholder attribute and Chosen will parse it automatically:
:data => {:placeholder => 'User Name'}

I found a better solution to this.
Following this https://github.com/harvesthq/chosen/issues/176, one can use .attr("data-placeholder", "my message here...").chosen(); instead of .data().

The right solution here is to place an empty option element on top.

To set a default text in the coffeescript file use:
$('.chzn-select').chosen
allow_single_deselect: true
no_results_text: 'No results matched',
placeholder_text_single: 'Please select an option',
placeholder_text_multiple: 'Please select some options'
Works for me with chosen-rails 1.5.2

Documentation says in the "Default Text Support" section that you should use data-placeholder attribute on the select element. I haven't tested it yet and I'm also curious how to do it from script not from the markup.
EDIT:
I've just found a solution: -- simple overriding data attribute from a script (pretty obvious :)).

Rails 5 - 2018
This code works with the simple_form gem:
<%= f.input :tag, as: :select, collection: Tag.all, input_html: {class: 'chosen-select', :data => {:placeholder => "your custom placeholder"}, multiple: true} %>

Related

Showing Link in Text Field in Rails

In my Rails 3.1 app, I have a text field for comments and I want to be able to allow people to include clickable links (instead of just the url showing as plain text), as well as having the text field recognize when a user had line breaks in their text field (without the user adding html). How can I do this?
This works for showing a link if a user puts the html for a href:
<%= simple_format(#user.description) %>
And this works for recognizing and displaying the line breaks from carriage returns in the text_field:
<%= h(#user.description).gsub(/\n/, '<br/>').html_safe %>
However, I haven't figured out how to do both, together.
How about this?
#Doesnt work in this case
#<%= simple_format( h(#user.description).gsub(/\n/, '<br/>') ).html_safe %>
EDIT:
Seems like you need auto_link function to achieve this. Though it is removed from rails 3.1 it is available as a gem. So if you are using rails 3.1 or later you need to get this from a separate gem
#in Gemfile
gem "rails_autolink", "~> 1.0.9"
#in application.rb
require 'rails_autolink'
#Run
bundle install
#now in you view use it like
<%= h auto_link(simple_format(text)) %>
auto_link not only converts urls but also email addresses in clickable link. Get the document here.
Reference Links:
http://rubygems.org/gems/rails_autolink
http://apidock.com/rails/ActionView/Helpers/TextHelper/auto_link
Use the Rinku Gem
Link is here.
It brilliantly solves the problem. Enjoy!

How can I use a model from a gem in Rails 3?

I have a gem that has some ActiveRecord-derived models. They are tested and work.
I've added a dependency to that gem, but when I try to access pages that refer to that model in a form_for statement, for example, I get the dreaded method_missing error:
undefined methodmygem_mymodel_path'`
Note I had the models in my app/models directory as is usual and all was well; migrating my models to this gem has been the cause for grief.
UPDATE 1:
In response to Robin's question:
> rails console
> MyModel
=> MyGem::MyModel(id:string, name:string)
Update 2: For Robin's request for form code
Form erb:
<%= form_for(MyModel.new, :remote => true, :html => { :class => "new_mymodel_form", :id => "new_mymodel_form"}) do |f| -%>
After Robin's suggestions, this is the only way I've found around it:
UPDATE 3: a ugly workaround
With a model called Mymodel and a module called MyModule:
post '/mymodels', to: 'mymodels#create', as: 'my_module_mymodels
Because 'as' let's you control the path symbol name. I'd much rather use 'resources' macro, but it seems it doesn't know to add the module name to the path symbol when it creates it, even though form_for appends the module name. I assume there is a way to solve this, but I can't find anything about this scenario.
Use scope:
scope :as => "mymodule" do
resources :my_resources
end

How to make rails 3 I18n translation automatically safe?

I use rails 3. Is there any easy way to tell I18n to respect 'html safness' of string used in interpolation and make all translated string html safe by default? So if I have this en.yml:
en:
user_with_name: 'User with name <em>%{name}</em>'
and I use t('user_with_name', :name => #user.name), I get users name html escaped, but <em> and </em> is left as is?
http://guides.rubyonrails.org/i18n.html#using-safe-html-translations
The official Rails guide says you can use the interpolated variables without concern, since they are html escaped automatically, unless you specifically declare them to be String.html_safe.
From the guide:
Interpolation escapes as needed though. For example, given:
en:
welcome_html: "<b>Welcome %{username}!</b>"
you can safely pass the username as set by the user:
<%# This is safe, it is going to be escaped if needed. %>
<%= t('welcome_html', username: #current_user.username %>
Safe strings on the other hand are interpolated verbatim.
Change the name from user_with_name to user_with_name_html, then rails will know you have included html in the text.
Old question, but if someone wants to achieve this, here's the monkey patch I came up with :
module ActionView
module Helpers
module TranslationHelper
private
def html_safe_translation_key?(key)
true
end
end
end
end
Put this in an initializers and that's it!
Works with Rails 3.2.6.
Only marks the text in localization files as safe, not the interpolation parameters.

Is there any rails QR code plugin for Rails 3?

I want to generate QR code for my application which is going to run in Rails 3.0,
Is there any plugin available for that ?
Thanks in Advance,
Jak.
You can use Google's Chart API
def generate_qr_image( url )
raw "http://chart.apis.google.com/chart?chs=150x150&cht=qr&chl=#{url}&choe=UTF-8"
end
I think Google changed the api end point.
This worked for me:
= image_tag "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=#{url}&choe=UTF-8"
As I understand it, anything based on google charts is deprecated, so not a long term solution.
I've used both the rqrcode gem and one of the many javascript encoders.
I Agree with TrAvid but it won't work, just need a small minute change,
Just assign #url="www.google.com" in the controller and then use this #url in View as
<%= image_tag "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=#{#url}&choe=UTF-8" %>
It worked for me great.
I use the rqrcode_png that allow you to save the code as an image or do it like a table. It also let you display the image without saving it.
Here is an example of how save the image
qr = RQRCode::QRCode.new( 'your string', :size => 1, :level => :h )
image = qr.to_img.resize(250,250)
image.save('name.png')
or if you don't want to save it you can
qr = RQRCode::QRCode.new( 'your string', :size => 1, :level => :h )
#png = qr.to_img.resize(250,250).to_data_url
and then in your view <%= image_tag #png %>

Multiple Haml Elements on Same Line

I want to be able to have two Haml elements on the same line. For example:
%h1 %a{:href => '/'} Professio.
That doesn't work. How would I get something like this to work without borking?
Late answer, but I was just tackling the same problem myself and knew HAML had a way to do this as part of the language. See the White Space Removal section of the HAML reference.
You could do it this way:
%h1<
%a{ :href => '/' } Professio.
Or this way:
%h1
%a{ :href => '/' }> Professio.
Unfortunately, you cannot put two haml elements on the same line.
You can achieve something similar by using inline html elements:
%h1 <a href='/'>Lorem ipsum</a>
Why don't you like this format?
%h1
%a{:href => '/'} Professio.
Another option is to write special 'helper' method (that generate an html link). For example, the link_to in Rails:
%h1= link_to 'Professio', root_url
Haml cannot do this. Slim can:
h1: a( href='/' ) Professio.
is the same as:
h1
a( href="/ ) Professio
You can write as deeper tree as you need:
ul.nav
li.active: a( href="/home" ): strong Home
li: a( href="/contact" ): span Contact
Jade also has similar syntax and support this feature, but it's been designed for Node.js environment.
If you're looking to preserve the HTML on the same line you could try something like this:
irb> print Haml::Engine.new("%h1<\n %a{:href => '/'} Profession.").render()
<h1><b href='/'>Profession.</a></h1>
Found here: HAML whitespace removal
[Edit: I know that's says b href above...]