Generic link_to in a shared partial

I am trying to write a simple and generic one table maintenance shared
partial which can be reused to maintain multiple tables with the
following fields:

id, name, created_by, updated_by.

I am stuck in generating rest route correctly. Here is an example for a
“crust_types” table inspired by Ryan B. Forms screencasts.

Here is the index action from CrustTypeController:

def index
@crust_types = CrustType.find(:all)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @crust_types }
end
end

Here is the content of corresponding index.html.erb template:

<%= render :partial => ‘shared/crud_table’, :object => @crust_types,
:locals => {:ll => ‘crust_type’} %>

Finally, here are the relevant code snippets from _crud_table.html.erb
partial in the shared folder:

<% for a_rec in crud_table %>
<tr class="<%= cycle(“odd”,“even”) %>">

<%= a_rec.id %>
<%= a_rec.name %>
<%= a_rec.created_at %>
<%= a_rec.updated_at %>
<%= link_to ‘Show’, “#{ll}path" %> | <%= link_to ‘Edit’,
"edit
#{ll}_path(#{ll})” %> | <%= link_to ‘Destroy’, “#{ll}_path” ,
:confirm => ‘Are you sure?’, :method => :delete %>

<% end %>

The rest paths for link_to methods are not being evaluated they are
literally output. For example: I want the link_to ‘Edit’,
“edit_#{ll}_path(#{ll})” method to generate the correct edit path as
'/crust_types/1/edit for the first record, instead, I get
‘/edit_crust_type_path(crust_type)’. Clearly, it is not being
evaluated. How should I do it?

Thanks in advance for your time.

Bharat

Try…

<%= link_to ‘Edit’, edit_path(rec) %>

I am not positive about “edit_path”, but you can run rake routes to
get the specific path.

-J

On Mar 20, 2:36 pm, Bharat R. [email protected]

Hello Jason,
Thanks for your response. Unfortunately that won’t work since the local
variable ll being passed to the partial determines which model are we
working with so in my example that will have to be
edit_crust_type_path(a_rec). It is the dynamic substitution of the
model name that I am getting stuck on. Unfortunately I do not know the
meta_programming magic required to make it work :slight_smile:
Anyway I have managed to do it the old-fashioned way by explicitly
spelling out the controllers and actions are shown below:

<% for a_rec in crud_table %>
<tr class="<%= cycle(“odd”,“even”) %>">

<%= a_rec.id %>
<%= a_rec.name %>
<%= a_rec.created_at %>
<%= a_rec.updated_at %>
<%= link_to ‘Show’, :controller => ll, :action => ‘show’, :id
=> a_rec.id %> |
<%= link_to ‘Edit’, :controller => ll, :action => ‘edit’, :id
=> a_rec.id %> |
<%= link_to ‘Destroy’, {:controller => ll, :action =>
‘destroy’, :id => a_rec.id} , :confirm => ‘Are you sure?’ %>

<% end %>

This works fine, but I would still like to know how to do it using
meta-programming.

Bharat

On Mar 20, 7:44 pm, acts_as_jason [email protected] wrote:

Try…

<%= link_to ‘Edit’, edit_path(rec) %>

I am not positive about “edit_path”, but you can run rake routes to
get the specific path.

Nearly! edit_polymorphic_path is the helper needed. For delete it’s
even easier, link_to ‘delete’, a_rec, :method => :delete.

Fred