Hi there, I am struggling to have an edit in place field for a select control. This select control picks its options from a database table. Googling got me some code examples. But none of them worked for me. They all seem to assume things. I''d appreciate if some body can point me at a complete code listing or ways to get this done. Thanks much in advance. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi ya
Try this...
### VIEW ###
<div class="form-row">
<div class="form-element-rowitem-wide label">Order
Status</div>
<div class="form-element-rowitem"
id="orderStatusBox"><%=-5j1vdhnGyZuzZXS1Dc/lvw@public.gmane.orgus
%></div>
</div>
<script type="text/javascript">
/* <![CDATA[ */
new Ajax.InPlaceCollectionEditor(
''orderStatusBox'',
"/orders/ajax_update_order_status/<%=params[:id]%>",
{
collection: [<%= map_statuses(@order_statuses) %>],
value: <%=-3TBsOvYkO2a5GZ74WTYKfg@public.gmane.org_status_id%>,
ajaxOptions: {method: ''post''}
});
/* ]]> */
</script>
### HELPER ###
def map_statuses(collection)
collection.map {|status| ''['' << status.id.to_s
<< '','' <<
"''#{status.status_name.to_s}''" << '']
,'' }
end
### CONTROLLER ###
@order_statuses = OrderStatus.find(:all)
---------------
I hope that helps
Cheers
Tim
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks Tim. It worked. To make things little neat, I moved this whole code into a helper method that returns me a javascript code. On 2/13/07, Tim Perrett <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi ya > > Try this... > > ###VIEW ### > > <div class="form-row"> > <div class="form-element-rowitem-wide label">Order Status</div> > <div class="form-element-rowitem" id="orderStatusBox"><%=@order.status > %></div> > </div> > > <script type="text/javascript"> > /* <![CDATA[ */ > new Ajax.InPlaceCollectionEditor( > ''orderStatusBox'', "/orders/ajax_update_order_status/<%=params[:id]%>", > { > collection: [<%= map_statuses(@order_statuses) %>], > value: <%=@order.order_status_id%>, > ajaxOptions: {method: ''post''} > }); > /* ]]> */ > </script> > > ### HELPER ### > > def map_statuses(collection) > collection.map {|status| ''['' << status.id.to_s << '','' << > "''#{status.status_name.to_s}''" << ''] ,'' } > end > > ### CONTROLLER ### > > @order_statuses = OrderStatus.find(:all) > > --------------- > > I hope that helps > > Cheers > > Tim > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> To make things little neat, I moved this whole code into a helper > method that returns me a javascript code.For sure :) In the spirit of this forum post up your helper - it might help somone in the future with any luck ;) Cheers Tim Perrett -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 28, 2007, at 8:18 AM, Love AJAX wrote:> Thanks Tim. It worked. > > To make things little neat, I moved this whole code into a helper > method that returns me a javascript code.If I might suggest a slight improvement: ### HELPER ### def map_statuses(collection) collection.map {|status| [status.id, status.status_name] }.to_json end Then you don''t have to worry about the potential problem of quotes within your status_name column. (''cause to_json will handle it for you) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> On 2/13/07, Tim Perrett <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> >> Hi ya >> >> Try this... >> >> ###VIEW ### >> >> <div class="form-row"> >> <div class="form-element-rowitem-wide label">Order Status</div> >> <div class="form-element-rowitem" id="orderStatusBox"><% >> =@order.status >> %></div> >> </div> >> >> <script type="text/javascript"> >> /* <![CDATA[ */ >> new Ajax.InPlaceCollectionEditor( >> ''orderStatusBox'', "/orders/ajax_update_order_status/<%=params[:id] >> %>", >> { >> collection: [<%= map_statuses(@order_statuses) %>], >> value: <%=@order.order_status_id%>, >> ajaxOptions: {method: ''post''} >> }); >> /* ]]> */ >> </script> >> >> ### HELPER ### >> >> def map_statuses(collection) >> collection.map {|status| ''['' << status.id.to_s << '','' << >> "''#{status.status_name.to_s}''" << ''] ,'' } >> end >> >> ### CONTROLLER ### >> >> @order_statuses = OrderStatus.find(:all) >> >> --------------- >> >> I hope that helps >> >> Cheers >> >> Tim--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here it is (modified to my needs):
#######################
def in_place_select_editor(options)
options[:ajax] = { :okText => "''Save''",
:cancelText =>
"''Cancel''"}.merge(options[:ajax] || {})
script = Array.new
script << "new Ajax.InPlaceCollectionEditor("
script << "
''#{options[:content][:options][:id]}'',"
script << "
''#{url_for(options[:url])}'',"
script << " {"
script << " collection: #{options[:collection]},"
script << options[:ajax].map{ |key, value| "#{key.to_s}:
#{value}" }.join(", ")
script << ", ajaxOptions: {"
script << "method: ''post''}"
script << " }"
script << ")"
content_tag(
options[:content][:element],
options[:content][:text],
options[:content][:options]
) + javascript_tag( script.join("\n") )
end
#######################
# In view:
<%= in_place_select_editor( :content => {
:element => ''span'',
:text => get_role_name(staff.login_id),
:title=> "",
:options => {
:id =>
"user_role_name_#{staff.id}",
:class =>
''editable-content''
}
},
:url => {
:controller =>
''user_lookup'',
:action => "set_permissiondata",
:id => staff.login_id,
:fieldname => ''role_id''
},
:ajax => {
:size => 15,
:value => "''" +
get_role_name(staff.login_id)+ "''"
},
:collection => roles
) %>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---