Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rails-jquery-autocomplete with Cells - undefined method `rewrite_autocomplete_option' #426

Closed
gaggle opened this issue Aug 18, 2016 · 12 comments

Comments

@gaggle
Copy link

gaggle commented Aug 18, 2016

Hi,

I'd like to use Cells, but run into problems with forms. I've overwritten dom_class/dom_id but I also want to use the rails-jquery-autocomplete gem in my form. But I get this error:

undefined method `rewrite_autocomplete_option' for #
<... Show::MembersCell:0x007fb1dee13628>

@Startouf already made an issue @ risuiowa/rails-jquery-autocomplete#58 in the autocomplete repo, but I wanted to create one here as well to see if someone knows of a workaround.

(currently on Rails 3.2, cells 4.0.5)

@apotonick
Copy link
Member

It's simple, you have to find the helper module that defines this method and include it in the cell.

Please also update to 4.1.

@Startouf
Copy link

Startouf commented Aug 18, 2016

Hey,
I believe the methods were tightly coupled to the rest of autocomplete classes, so I ended up creating an autocomplete helper especially for cells where I basically sort of copy pasted the relevant autocomplete code

def rewrite_autocomplete_option(options)
    options["data-autocomplete-fields"] = JSON.generate(options.delete :fields) if options[:fields]
    options["data-update-elements"] = JSON.generate(options.delete :update_elements) if options[:update_elements]
    options["data-id-element"] = options.delete :id_element if options[:id_element]
    options["data-append-to"] = options.delete :append_to if options[:append_to]
    options
  end

I also wrote my own form helper on top of that

# Produces an autocomplete field that can populate an id element
  # = Params
  # +association+ - symbol for the association
  # +path+ - url used for getting autocompletion results
  # = Options
  # +:autocomplete+ - default to false (the html_option autocompletes from previous output)
  # +:id_element+ - change the populated id field, or set to nil to do nothing
  def autocomplete_association(f, association, path, options={})
    # Da magic regexp
    id_element = "##{f.object_name}[#{association.to_s}_id]".gsub(/(\])?\[/, "_").chop
    options[:autocomplete] ||= false
    options[:id_element] ||= id_element
    capture do
      concat f.hidden_field("#{association.to_s}_id")
      concat f.autocomplete_field(association, path, options)
    end
  end

So then I could call the autocompleter easily in my views

<%= autocomplete_association(f, :main_school, autocomplete_school_path, value: f.object.main_school_name,
            placeholder: t(:add_school_placeholder),
            class: 'form-control')
        end %>

...which would fill both the field with the visual name for the user, and the hidden id field with the id element for database saving purposes.

@gaggle
Copy link
Author

gaggle commented Aug 18, 2016

Thank you both for the extremely fast replies!

@Startouf thank you for the code snippet, I'm too new to Ruby/Rails to figure this out myself and your code brought me further. In fact, it solved the issue!... ish. I'm now stuck on Cells generating incorrect form elements where input elements are siblings to the form element, as described in #260. I can make a new issue to pursue a solution for this, but I take it you don't have this problem?

@apotonick I've experimented with an upgrade but there's a version conflict error when I just target cells so I've only successfully upgraded by deleting the .lock file which also lets in a ton of other updates. I'm introducing Cells to better manage our complexity and put us on a sane(er) upgrade path, but for now I'm not sure I can proceed.. Does 4.1 specifically address issues surrounding forms?

@Startouf
Copy link

@gaggle The new issue you raised seem to concern only HAML but I am using ERB. No problems here anyways. Don't forget to include cells-rails (Cells hacks for Rails) and cells-(Haml/erb) into your Gemfile.

Amongst others, I have

include ActionView::Helpers::FormHelper
include ActionView::Helpers::FormOptionsHelper
include AbstractController::Helpers

inside cells that need forms

@gaggle
Copy link
Author

gaggle commented Aug 18, 2016

@Startouf I see, I didn't know it just affected HAML, that is indeed what we use. I've been unable to include cells-rails gem because it requires cells 4.1, and the furthest I've gotten with 4.1 is that the server starts but cell helper raises wrong constant name for all existing cells (so far I've been using version 4.0.5 and just cells-haml gem).

There's probably some way through all this that I don't know about yet, but unless you're spotting something obvious I don't mean to drag you into debugging our setup. For now I'll park my Cells branch and loop back to this when I can upgrade with confidence.

@apotonick
Copy link
Member

@gaggle The "upgrade" to 4.1 should be seamless, you only need to bump up cells to 4.1 and gem "cells-rails". What exception are you facing?

I really encourage you to stay up-to-date! Jump on our Gitter channel for quick support. https://gitter.im/trailblazer/chat

@gaggle
Copy link
Author

gaggle commented Aug 22, 2016

@apotonick, thanks I'll check out the gitter channel. When I add version constraint and cells-rails alá

gem 'cells', '~>4.1'
gem 'cells-haml'
gem 'cells-rails'

Then doing a bundle upgrade cells results in errors such as:

Bundler could not find compatible versions for gem "tilt":
  In Gemfile:
    rails (= 3.2.18) ruby depends on
      tilt (!= 1.3.0, ~> 1.1) ruby

    cells (>= 4.1) ruby depends on
      tilt (2.0.5)

Is Cells supposed to work with Rails 3.2? Because I assumed this is on us, that we need to get with the times.

@apotonick
Copy link
Member

It should work with 3.2 - I actually don't know about the Tilt compat, but you could try setting the cells tilt dependency to 1.1, this should still work.

@gaggle
Copy link
Author

gaggle commented Sep 1, 2016

Hi @apotonick,

I plucked away at this, and have managed some progress but ultimately no success.

To recap, I downloaded the cells and cells-haml gems, and changed their dependencies to accept lower tilt and haml dependencies respectively. This allowed bundler to upgrade:

  cells (~> 4.1)!
  cells-haml!
  cells-rails

But the result is the same as my previous attempt, pages that uses widgets raise a wrong constant name error (and these pages worked prior to the upgrade).

The locally-altered-hack is not sustainable in the long run anyway, so I'll be backing out of this and phasing out our use of Cells for the time being. Maybe once we complete our upgrades I can loop back around. Thanks for the help, and of course if you spot something I should try I'm happy to keep experimenting in my branch.

@apotonick
Copy link
Member

@gaggle Can you show me one of those "missing constant" exceptions and the way you invoke the cell helper?

@gaggle gaggle closed this as completed Jan 18, 2017
@gaggle
Copy link
Author

gaggle commented Jan 18, 2017

Closing as I have stopped using Rails.

@apotonick
Copy link
Member

Good decision. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants