Topic: Yoda Partials

I couldn't think of any other name for this thread tongue

I need my partials to do some wacky kung-fu that is beyond my jedi skills.

Basically I have this admin_widget that is getting sprinkled all over my app.

Everything is cool until I try to place edit or delete links inside this partial.

# My Partial
<%= link_to "Edit #{@current_controller.singularize}", edit_item_path(item) %>
<%= link_to 'Delete', item_path(item), :confirm => 'Are you sure?', :method => :delete %>
# My Partial Call
<%= render_partial 'shared/admin_widget', nil, 'item' => task %>

I am really struggling with two problems here

1. How to get something like edit_item_path to be dynamic (in this case it would become edit_task_path)
2. How to set this up so that it jives with both index (local_variable) and show (@instance_variable) actions.

I need the Force!

Last edited by pimpmaster (2007-05-09 12:27:51)

Re: Yoda Partials

I got something working.. it aint too pretty but I am proud of how DRY my templates are now

#Partial Call
<%= render :partial => 'shared/admin',
           :locals => { :item => term,
                 :edit => edit_term_path(term),
                 :delete => term_path(term) } %>
#Partial
<%= link_to "Edit #{@current_controller.singularize}", edit %>
<%= link_to 'Delete', delete, :confirm => 'Are you sure?', :method => :delete %>

Oh yeah...score one for the Rebels!

Re: Yoda Partials

Good job jedi.

You might want to skip the named routes and just use a hash:

<%= link_to "Edit #{item.class.name.titleize}", :controller => item.class.name.pluralize.underscore, :action => 'edit', :id => item %>

Railscasts - Free Ruby on Rails Screencasts

Re: Yoda Partials

The master has spoken.

http://www.igvita.com/blog/2007/03/15/b … -in-rails/

Its a little overkill for what I want to do here, but this is definitely going in the  toolbox!

Edit: You posted right when I did Ryan. Either the Force is trying to tell us something, or we spend far too much time thinking about Rails. tongue

Last edited by pimpmaster (2007-05-09 14:09:20)

Re: Yoda Partials

I think it's telling me I spend far too much time on this board... back to work....

Railscasts - Free Ruby on Rails Screencasts

Re: Yoda Partials

Yes folks, even Jedis have to pay the bills.

You do your thang buddy. I'm gonna spend the next few hours figuring out how to lighten the DB overhead of these dang partials. I tried to get silly and incorporate the userstamp we worked on in the other thread, now I got DB queries every time the partial is rendered and its crampin my style!

Rendered shared/_admin (0.00742)
User Load (0.002348)   SELECT * FROM users WHERE (users.`id` = 0)
  User Load (0.000815)   SELECT * FROM users WHERE (users.`id` = 1)
Rendered shared/_admin (0.00940)
  User Load (0.001167)   SELECT * FROM users WHERE (users.`id` = 0)
  User Load (0.000801)   SELECT * FROM users WHERE (users.`id` = 1)
Rendered shared/_admin (0.00950)
  User Load (0.000836)   SELECT * FROM users WHERE (users.`id` = 0)
  User Load (0.000865)   SELECT * FROM users WHERE (users.`id` = 1)
Rendered shared/_admin (0.01114)
  User Load (0.000819)   SELECT * FROM users WHERE (users.`id` = 0)
  User Load (0.000841)   SELECT * FROM users WHERE (users.`id` = 1)

# ETC ETC ETC


ugh!

I tried eager loading but it really wasn't much faster (and you dont even want to see the server output for that).

At this point I am rethinking the whole implementation of updated/created_by. I know its not very Rails-ish, but I just may store the user.name in the database instead of the user.id. It certainly runs a lot faster than this..

Re: Yoda Partials

pimpmaster wrote:

At this point I am rethinking the whole implementation of updated/created_by. I know its not very Rails-ish, but I just may store the user.name in the database instead of the user.id. It certainly runs a lot faster than this..

I would still keep the id so you can reference other user parameters if you need to. But you can still cache the user name in addition to that. Perhaps you can build this into the method so it automatically caches it upon saving/updating the model.

Railscasts - Free Ruby on Rails Screencasts

Re: Yoda Partials

That is freakin smart. Tomorrow is officially deploy day for me so I want to definitely run more tests and play with your subdomain idea along with caching, if all goes well. I'm gonna be rocking deprec gem on my slicehost.. wish me luck!