Forum: Ruby on Rails Generic link_to in a shared partial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D3fc5887a2f39f2e0c8989d39ce5e6f9?d=identicon&s=25 Bharat Ruparel (bruparel)
on 2009-03-20 20:36
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 Bates 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") %>">
      <td class="first"><%= a_rec.id %></td>
      <td><%= a_rec.name %></td>
      <td><%= a_rec.created_at %></td>
      <td><%= a_rec.updated_at %></td>
      <td><%= link_to 'Show', "#{ll}_path" %> | <%= link_to 'Edit',
"edit_#{ll}_path(#{ll})" %> | <%= link_to 'Destroy', "#{ll}_path" ,
:confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% 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
F18deefddb4a1e1324cd2f0b6d14bd5c?d=identicon&s=25 acts_as_jason (Guest)
on 2009-03-20 21:34
(Received via mailing list)
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 Ruparel <rails-mailing-l...@andreas-s.net>
D3fc5887a2f39f2e0c8989d39ce5e6f9?d=identicon&s=25 Bharat Ruparel (bruparel)
on 2009-03-20 21:42
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 :)
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") %>">
      <td class="first"><%= a_rec.id %></td>
      <td><%= a_rec.name %></td>
      <td><%= a_rec.created_at %></td>
      <td><%= a_rec.updated_at %></td>
      <td><%= 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?' %></td>
    </tr>
  <% end %>

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

Bharat
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-21 00:28
(Received via mailing list)
On Mar 20, 7:44 pm, acts_as_jason <jason.by...@gmail.com> 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
This topic is locked and can not be replied to.