I have a UserMailer View that has several link_to's like so:
<开发者_开发问答%= link_to('XXXXXXXX Link Title', item_url(@item, :only_path => false), :style => 'color:#5196E3;text-decoration:underline;') %>
The page has several different links. I'd like to know if there is a way to globally set in the view to enable or disable the links.
If enabled, the above would run like normal, if not the block above would just show the text (XXXXXXXX Link Title) and not be linked?
Any ideas other than wrapping every link_to inside a IF statement?
Thanks
Rails already provides a link_to_if
helper...
So, define @some_boolean
in the controller class, or if you want a true global, then set $some_boolean
appropriately. Then use the link_to_if
:
<%= link_to_if(@some_boolean, "Link Title", <url etc..>) %>
Documentation
you can create a helper method that takes your link parameters and returns the value that you want. which means you will only implement one IF statement.(which will be in the helper.)
great comment by Sean Hill: helpers should be in helper files :)
ApplicationHelper:
helper_method :conditional_link
def conditional_link(string,url)
if true_condition
return link_to string, url
else
return string
end
in your view:
<%= conditional_link string, url %>
if also want else part if condition not true then use this
<%= link_to_if( some_boolean, "Link Title", <url etc..>) { link_to "link title", <url etc..>} %>
remember if you wont use code block then text will appear and if u want to render link in other style in case of false then use code block.
精彩评论