Topic: Cleaning my views

Hi guys,

I am currently trying to clean up my views. As you can see in the last
example below, I'm having a new link to create a new resource
dynamically. The controller is given by the params[:controller]. This
way I create new action paths to all my resources with the same code.

I want now a similar thing but for the show action. The thing is that
I'll need the instance variable. How can I set the instance variable
dynamically for a show action. I'll have to add to the url_for method
below the 'show' parameter with the instance variable that comes from
the controller (example: @user, @message, @foo).

I tried the following first, but it didn't worked since the id is then
processed as a string "@user", so I get the following url: /users/
@user. I hope you guys can help me trying to solve this one. Thanks!

url_for(:controller => params[:controller], :action => "show", :id =>
"@"+params[:controller].singularize)
<div>
  <% link_to url_for(:controller => params[:controller], :action =>
"new") do %>
    <%= image_tag("add.png") %>
    <span><%= t('toolbar.new') %></span>
  <% end %>
</div> 

Re: Cleaning my views

I found this and it worked:

:id => instance_variable_get("@#{params[:controller].singularize}")

One more thing though. When I try to do the link_to dynamically, I generated the link_to urls using the url_for method. Is there any other way to generate this routes dynamically without using the url_for method? Thanks!

Re: Cleaning my views

This seems massively overly complicated.  For a start, you don't need to pass the actual variable through.  All the 'id' param needs is the id of the object.  As a convenience, rails lets you pass the whole object through, which then has 'to_param' called on it, which returns the id by default.  So, you can cut out the middle man and just give it the id if you want.

Also, if you've already go the variable, why are you trying to dynamically create it (in a really weird way)?  There's nothing special about the name @resource, it's just a variable that holds an active record object.  You could call it whatever you want.  All this 'instance variable get' stuff is crazy, why not just refer to the instance variable directly, if you have it already.

You should have defined this resource (users, or whatever) in your routes.rb. so that you have a path to the show action, which takes the object.  eg  "user_path(@user)".  just use that:

link_to user_path(@user)

I get the feeling that you don't really understand how rails (and maybe even ruby) work, i think you should spend a little bit more time reading up on ruby before diving straight into rails.

Last edited by Max Williams (2009-09-21 07:11:54)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Cleaning my views

Max Williams wrote:

This seems massively overly complicated.  For a start, you don't need to pass the actual variable through.  All the 'id' param needs is the id of the object.  As a convenience, rails lets you pass the whole object through, which then has 'to_param' called on it, which returns the id by default.  So, you can cut out the middle man and just give it the id if you want.

Also, if you've already go the variable, why are you trying to dynamically create it (in a really weird way)?  There's nothing special about the name @resource, it's just a variable that holds an active record object.  You could call it whatever you want.  All this 'instance variable get' stuff is crazy, why not just refer to the instance variable directly, if you have it already.

You should have defined this resource (users, or whatever) in your routes.rb. so that you have a path to the show action, which takes the object.  eg  "user_path(@user)".  just use that:

link_to user_path(@user)

I get the feeling that you don't really understand how rails (and maybe even ruby) work, i think you should spend a little bit more time reading up on ruby before diving straight into rails.

I think you didn't understand what I was trying to do. I'm doing a partial view for new, delete and edit actions for various resources (using only one layout) So the new, edit, and delete actions must link_to an specific resource depending on where the user navigates. With this method I'll be creating links to various resources (ex: posts, users, messages) dynamically. I can't specify something like user_path(@user), because that will always link me to the users resource, those links have to be generated dynamically. Hope this clarified it for you! Thanks

Re: Cleaning my views

err, why?  This sounds like a recipe for confusion and trouble to me...you can use one layout for many different resource types but i'd strongly recommend keeping the individual partials for each type of resource rather than trying to write code that dynamically generates variable names and methods.  You're going to get yourself into a right mess...

Last edited by Max Williams (2009-09-22 04:48:33)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Cleaning my views

Max Williams wrote:

err, why?  This sounds like a recipe for confusion and trouble to me...you can use one layout for many different resource types but i'd strongly recommend keeping the individual partials for each type of resource rather than trying to write code that dynamically generates variable names and methods.  You're going to get yourself into a right mess...

Yep. +1 for that.