rails 3.1 auto-completion and realtime update in rails - autocomplete

Can anyone tell me how to easily implement auto-complete in rails 3.1?thanks. p.s. What's the best solution for simple_form to do this??
Besides, I also want to know how to implement meetup's RSVP instant push in rails?
I have the following form, but it didn't work, please advise.
%table
= simple_form_for(#book) do |f|
%th book name
%th
= f.input :name, label: false, placeholder: "add a name", :wrapper_html => { :id => 'autocomplete' }
= f.input :object, autofocus: true, label: false
%th
= f.button :submit, "Yes!"

I built a simple Rails 3.1 app using the jQueryUI autocompletion feature here. You simply add the jQueryUI library to your assets, add a css class or id to the actual text field that shall be autocompleted and put some javascript logic in your application, like:
$(function() {
$('#autocomplete').autocomplete({
source: '/movies/autocomplete'
});
});
with #autocomplete the css id of the text field. '/movies/autocomplete' is the actual route that will HTTp requested with the data that's put into the text field. In the movies#autocomplete action you then use that param to search for data for the autocompletion. Depending in what kind of jQueryUI autocompletion you use, you simply return an array like [{:label => 'FOO', :value => 'foo'}, ...] as JSON. label is the text you get autocompleted, value is the text that is put into the text field when clicking the label. I think and hope the code from the repo is quite understandable. Also, have a look at the JQueryUI library.
Though there shouldn't be a problem with simple_form, since it's just the text field that gets touched.
Best regards
Tobias

Related

Why doesn't simple form with bootstrap generate bootstrap-compatible forms?

I follow the API instructions, but the form behavior doesn't output what it says it should in the example and example source code.
I installed simpleform and bootstrap.
I did rails g simple_form:install --bootstrap
I see that the files have been generated.
Simpleform says to do code for an element like so
= simple_form_for #park, html: { multipart: true, role: "form", class: "form-horizontal" } do |f|
- if #park.errors.any?
#error_explanation
%h2= "#{pluralize(#park.errors.count, "error")} prohibited this park from being saved:"
%ul
- #park.errors.full_messages.each do |msg|
%li= msg
= f.input :address, label: 'address', autofocus: true, placeholder: "123 Fake Street Irvine, CA 90123", class: "form-control"
The layers of DSLs for simple output of forms is getting kind of ridiculous.
Many things wrong with the output
1) the wrapping classes don't match bootstrap's form-group, and instead it's still controls
2) the form doesn't stretch out 100% width like it should.
3) If an input isn't detected as "required", the label doesn't get wrapped, and it sticks off to the left of the screen.
Why isn't the API working?
The simple form generator command with bootstrap generates config initializers files for bootstrap 2.x
Thus you must modify the initializer files to spit out appropriate classes.
See the answer to this question.
Bootstrap3 and simple form

Rails: Form in static pages of high voltage gem

I have created few static pages with awesome high_voltage gem.
My pages are static, except a different form in each page.
Forms are supposed to grab user details and send emails (and may be save details in db)
Question:
How should I proceed with this ?
I am concerned more about rails approach,
e.g.
How can I add validation without custom coding (both at client / server side)
How can take help from rails helper method ?
Suppose, I would like to save those fields in db, how should I deal with that ?
In nutshell, I want to use as maximum of rails magic without manually dealing with things.
What I have tried ?
Currently, I have forms in each page. Each form posts at same controller (PagesController) with post method.
I then differentiate them, based on hidden input on respective form.
I do not have any validations for now.
I finally created a model without activerecord, following this great article.
I created a model like below.
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :subject, :body
validates :name, :email, :subject, :body, :presence => true
validates :email, :format => { :with => %r{.+#.+\..+} }, :allow_blank => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
I have answered this post in more detail here:
Render partial from static page
Here is the commit to a demo repository to make this work:
https://github.com/harlow/high_voltage_demo/commit/0cdbca5b0fe898d27df22787498fc7e350e81422

TinyMCE and ActiveAdmin for Rails

I am reacquainting myself with Rails and I am really liking Active Admin. I would like to get tinyMCE working with it for use in text areas. However, any instructions I find are incomplete. For some reason, I think that I am missing something really simple here.
So, for example, I have tinymce-rails installed (3.4.9) and followed the instructions (https://github.com/spohlenz/tinymce-rails). However, here's where I think I failed: actually initiating tinyMCE. According to the doc, I have two options:
use the <%= tinymce %> helper or...
initialize it like the following tinyMCE.init({
mode: 'textareas',
theme: 'advanced'
});
I tried adding the latter to my active_admin.js file to no avail.
If someone could guide me on this, I would be most appreciative.
I got it working doing the following things (outside of the install described at the repo)
In admin/my_class.rb:
ActiveAdmin.register MyClass do
form do |f|
f.inputs do
f.input :body, :input_html => { :class => "tinymce" }
end
end
end
In initializers/active_admin.rb:
...
config.register_javascript 'tinymce.js'
This was what actually got the tinymce.js script to show up in the head of the admin layout.
In javascripts/active_admin.js:
//= require active_admin/base
//= require tinymce
$(document).ready(function() {
tinyMCE.init({
mode: 'textareas',
theme: 'advanced'
});
});
After doing those things, that body input (text area) had a fully functioning editor on it.
Do your textarea inputs have a 'class' attribute or something that tinyMCE can hook into? Does it work from the javascript console (firefox/chrome)? Have you checked the presence of tinymce.js in the head(source) of your page.
I got it working with a form partial, and I had to give a class to the input element so tinymce could hook to it.
<%= f.input :literature_nld, :input_html => { :class => 'tinymce', :size => "80x4" } %>
Good luck

drupal 7 form validate add class?

In my form I create a checkbox
$form['existing_customer'] = array(
'#type' => 'checkbox',
'#title' => t('Are you an existing customer?')
);
When I validate it using hook_validate I would like to add a class to the label? Any ideas how to achieve this?
I can't imagine why you'd want to do this in a validation function, and I think there's a far easier way to accomplish what you're trying to do.
Each element in a Drupal form is wrapped with a container (which has an ID). Inside this container there will only ever be one label.
So if you need to target the element in CSS or JS you just need to do something like this:
#existing-customer-edit label {
// The rule
}
OR
$('#existing-customer-edit label').something();
If you really need to edit the label manually then you're going to have to provide a custom theme for that element, have a look at this example for more information (it's for Drupal 6 but the concept is the same in Drupal 7).
thanks Clive did a fairly nasty work around in the form validation function
$form_state['complete form']['myselectbox']['#title'] = '<span class="privacy-error">you did not check me</span>';
It ain't pretty but it works!
You can add a class in hook_validate():
$form_state['complete form']['submitted']['existing_customer']['#attributes']['class'][] = 'class_name';

Asp MVC 2: Typed Editor Template

(I reference this tutorial in this text)
I want to use the Html.EditorFor (or Html.Editor) helpers.
If a UserControl needs additional data it is passed via
...EditorFor(model => model.Album, new { Artists = Model.Artists, ... })
In the UserControl it's accessed via ViewData[stringKey], ie
... new SelectList(ViewData["Artists"] as IEnumerable, ...
To me this smells a little fishy as I would prefer a strongly typed ViewModel which ensures that specific data is available.
I'm now a little bit stuck as I don't know wheater there's a "typed way" to find or I should accept this way as-is.
How did you solve this issue? Any help appreciated!
Lg
warappa
I would probably change my view model so that I don't need to pass this additional information. You could make for example an album has a collection of artists. Now all tha you will have to do is:
<%: Html.EditorFor(model => model.Album) %>
And in your editor template:
<%: Html.DropDownListFor(x => x.SelectedArtist, new SelectList(Model.Artists)) %>