user.rhtml
1 2 3 4 5 6 7 8 9 10 11 12
<% if user.type == "Normal"%> <%= link_to ("Normal",{ :controller => "admin", :action => "Normal", :id =>howt.id },:class=>"bluelinks-allsmall") %> <% else %> <%= link_to ("Executive",{ :controller => "admin", :action => "Executive", :id =>how.id },:class=>"bluelinks-allsmall") %> <% end %>
refactored user.rhtml
1 2 3 4 5 6 7 8
<%=link_to_if(user.type == "Normal", ("Normal",{ :controller => "admin", :action => "Normal", :id =>how.id },:class=>"bluelinks-allsmall") }) do link_to ("Executive",{ :controller => "admin", :action => "Executive", :id =>how.id },:class=>"bluelinks-allsmall") end %>
Refactorings
No refactoring yet !
DG
August 20, 2008, August 20, 2008 10:32, permalink
I did some refactoring. Please forgive me If what I done is wrong.
- Following method can be used any where where we need if else.
- I like rails link_to_if method also but just I don't like writing link_to,:controller and other parameters twice.
- I know that helper method is not that much scalable. I just threw my idea may be you guys can help me.
Thanks
DG
user.rhtml
1 2 3 4 5 6
<%= link_to_condition( user.type == "Normal", ["Unapprove","Approve"], {:controller => ["admin","admin"], :action => ["Normal","Executive"], :id =>[how.id,how.id] },:class=>["bluelinks-allsmall","bluelinks-allsmall"]) %>
ApplicationHelper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# This method needs solid refactoring and test cases. module ApplicationHelper def link_to_condition(condition,name,options={},html_options={}) name = condition ? name[0] : name[1] controller = condition ? options[:controller][0] : options[:controller][1] action = condition ? options[:action][0] : options[:action][1] id = condition ? options[:id][0] : options[:id][1] html_class = condition ? html_options[:class][0] : html_options[:class][1] link_to (name,{ :controller => controller, :action => action, :id =>id },:class=>html_class) end end
Adam
August 20, 2008, August 20, 2008 13:51, permalink
You should be using named routes, not hashes, for URLs. After you change that it won't be so bad to have just a plain old if statement in your view.
View
1 2 3 4 5 6 7
<span class="bluelinks-allsmall"> <% if user.normal? %> <%= link_to "Normal", formatted_howto_path(howto, :normal) %> <% else %> <%= link_to "Executive", formatted_howto_path(howto, :executive) %> <% end %> </span>
Chris Hunter
August 20, 2008, August 20, 2008 13:54, permalink
This may not be scalable depending on what user.type can be, but it works for the case presented and is much DRYer. I've used a lowercase action name since it goes against Ruby/Rails convention to use uppercased method names.
1
<%= link_to (user.type, { :controller => "admin", :action => user.type.downcase, :id =>howtoguide.id },:class=>"bluelinks-allsmall") %>
Krist0ff
August 21, 2008, August 21, 2008 12:54, permalink
a little modification, if it should be 'Executive' for any other type than normal
1 2 3
<% type = user.type == 'Normal' ? 'Normal' : 'Executive' %> <%= link_to(type, { :controller => "admin", :action => type.downcase, :id =>howtoguide.id },:class=>"bluelinks-allsmall") %>
Hi,
Are there any other best ways?.
Thanks
DG