Finally solved this problem after digging deep into Rails'' innards. Am
noting solution here for those who may run into the same thing. There
are probably better ways, but this worked for me.
the in_place_select_editor_field calls the IntanceTag.to_content_tag
method, which is as follows:
def to_content_tag(tag_name, options = {})
content_tag(tag_name, value, options)
end
def value
unless object.nil?
object.send(@method_name)
end
end
So as you can see, it just assumes that the value to be displayed in the
tag is the value of the method passed to the tag.
So I extended the InstanceTag class by adding a new
''to_content_tag_display'' method. I added the following to my
application_helper.rb (maybe there''s a better place to put this kind of
stuff? I don''t really know.)
module ActionView
module Helpers
class InstanceTag #:nodoc:
include Helpers::TagHelper
def to_content_tag_display(tag_name, display, options = {})
content_tag(tag_name, display, options)
end
end
end
end
Then I modified my in_place_select_editor_field to accept an additional
display parameter and to call to_content_tag_display passing to it the
same additional parameter:
def in_place_select_editor_field(object, method, display, tag_options
= {},
in_place_editor_options = {})
tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)
tag_options = { :tag => "span",
:id =>
"#{object}_#{method}_#{tag.object.id}_in_place_editor",
:class =>
"in_place_editor_field"}.merge!(tag_options)
in_place_editor_options[:url] in_place_editor_options[:url] ||
url_for({ :action => "set_#{object}_#{method}", :id =>
tag.object.id })
tag.to_content_tag_display(tag_options.delete(:tag), display,
tag_options) +
in_place_select_editor(tag_options[:id], in_place_editor_options)
end
Then in my view, I added an additional parameter:
<%= in_place_select_editor_field(
:item,
:language_id,
@item.language.lang,
{},
:select_options =>
options_from_collection_for_select(@languages, ''id'',
''lang'')) %>
the third parameter (in this case @item.language.lang) is the value to
be displayed.
PS. This might make a useful patch?
Fran?ois Montel wrote:> I''m attempting to use the example given in the Rails Recipes book
> chapter 2,
> to create a select drop-down using the InPlaceEditor function (modified
> to
> be InPlaceSelectEditor.
>
> The example given works (a country list drop-down), but I''m
attempting
> to
> use it to display a list of items generated from an object in another
> table
> that is linked to the table that I''m editing. That also works
except
> that it
> displays the ''id'' instead of the related value of the id
in the other
> table.
> Sorry if that isn''t clear. Here''s the relevant code.
>
> Table item contains language_id (among other fields, of course)
> Table language contains id, lang (the latter having the language in
> English)
>
> <% for @item in job.items %>
> <tr>
> <td><%= in_place_editor_field :item, :title
%></td>
> <td><%= in_place_select_editor_field(
> :item,
> :language_id,
> {},
> :select_options =>
> options_from_collection_for_select(@languages, ''id'',
''lang'')) %>
> </td>
> ( ... more code ... )
> </tr>
> <% end %>
>
> It works okay except that it displays item.language_id (which when
> clicked,
> correctly shows the list of languages, not their ids), when what I want
> it
> to display is item.language.lang (not the id of the language). However,
> the
> in_place_select_editor helper doesn''t seem to have on option to
override
> the
> value that is displayed. It just automatically displays the
> object/method of
> the first two options. I presume I could extend the helper to take an
> additional option that would be displayed, but I don''t know any JS
so
> I''m
> not sure how to do that. (Sorry, I''m a beginner.)
>
> Thanks for the help!
--
Posted via http://www.ruby-forum.com/.