bernat / best_in_place

A RESTful unobtrusive jQuery Inplace-Editor and a helper as a Rails Gem
http://blog.bernatfarrero.com/in-place-editing-with-javascript-jquery-and-rails-3/
1.2k stars 571 forks source link

how to pass parmeters to display_with => :custom_method #494

Open sanjusoftware opened 9 years ago

sanjusoftware commented 9 years ago

I have the following code:

= best_in_place req, :looking_for_list, :url => user_request_path(user, req)

the method 'looking_for_list' returns me an array of string values e.g ["coffee", "tea"]. I want to display these values with hash tags prepended to each e.g #coffee #tea

to achieve this I did the following: = best_in_place req, :looking_for_list, :url => user_request_path(user, req), :display_with => lambda { |tags| tags.map{|tag| "##{tag}"}.join(' ') }

In order to make it more reusable and readable I wanted to move the lamda to a helper function, so that I could do the following:

= best_in_place req, :looking_for_list, :url => user_request_path(user, req), :display_with => hash_tags

And I added a method in my application helper class like:

def hash_tags(tags) tags.map{|tag| "##{tag}"}.join(' ') end

However, this code gives me error "wrong number of arguments (0 for 1)"

My question is: how to pass the looking_for_list parameter to the hash_tags helper method?

buddye commented 9 years ago

Ran into this also, your helper method needs a 2nd parameter for the helper_options hash. In case anyone reading this needed an example, say you wanted to wrap those tags in a label or span html tag and wanted to specify which in your view (like a Bootstrap css tagged label, span or other tag).

def hash_tags(tags, helper_options = {})
    default_helper_options = {content_tag: :span, content_tag_class: '', prepend_tag: ''}
    options = default_helper_options.merge(helper_options)
    tags.map { |tag| content_tag(options[:content_tag], "#{options[:prepend_tag]}#{tag}", class: options[:content_tag_class]) }.join(' ').html_safe
end

Then in your view you could use something like = best_in_place req, :looking_for_list, url: user_request_path(user, req), display_with: :hash_tags, helper_options: {content_tag: :label, content_tag_class: 'label label-info', prepend_tag: '#'} or = best_in_place req, :looking_for_list, url: user_request_path(user, req), display_with: :hash_tags, helper_options: {content_tag: :kbd}

sadiksha commented 8 years ago

Did you solve this already? I tried to use it like @EthanEtienne said, but I get an error saying undefined method helper

buddye commented 8 years ago

@sadiksha I end up monkey patching, got info here http://stackoverflow.com/a/13562078

module ActionView
  class Base

    def hash_tags(tags, helper_options = {})
      default_helper_options = {content_tag: :span, content_tag_class: '', prepend_tag: ''}
      options = default_helper_options.merge(helper_options)
      # tags = tags.split(" ")
      tags.map { |tag| content_tag(options[:content_tag], "#{options[:prepend_tag]}#{tag}", class: options[:content_tag_class]) }.join(' ').html_safe
    end

  end

end
sadiksha commented 8 years ago

Hi, I got what i needed using following: <%= best_in_place entry, :open, as: :checkbox, collection: { true: '<span class="label label-danger">Open</span>'.html_safe, false: '<span class="label label-success">Closed</span>'.html_safe }, classes: 'open-checkbox' %>

It would have been awesome to get helper method to get collection things, but somehow the helper didn't work.