# Spring Cleaning

**URL:** <https://www.ruby-forum.com/t/spring-cleaning/187275>\
**Category:** Rails\
**Created:** [April 20, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/spring-cleaning/187275 "2010-04-20T23:20:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 20, 2010, 11:20pm UTC](https://www.ruby-forum.com/t/spring-cleaning/187275/1 "2010-04-20T23:20:10Z")

</div>

Right now in my view, I have a whole HUGE clump of

\<% if current\_user %\> and then \<% if current\_admin %\> and then \<% if  
current\_teacher %\> and then all that other stuff in my view.

I have a ginourmous chunk of if and else statements in my views… is  
there a way to make it prettier? The if and else’s almost all have the  
same functions, expect for some minor changes.

Like this-

\<% if current\_user %\>  
\<%= link\_to “Create a new BLAH”, new\_blah\_path %\>

\<% end %\>

\<% if current\_admin %\>  
\<%= link\_to “Create a new ADMINBLAH”, new\_adminblah\_path %\>

\<% end %\>

BUT I have a LOT LOTLOT of them… all basically doing the same  
things, with the path changed.

Now, in this situation, how would david h hansson do it? 🙂 Would he  
create a helper of some sort? Because I feel like I’m repeating myself  
a lot in the code. What happened to keeping it DRY ??? 😛

So how would i go about doing this?

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 21, 2010, 12:00am UTC](https://www.ruby-forum.com/t/spring-cleaning/187275/2 "2010-04-21T00:00:25Z")

</div>

David Z. wrote:

> Right now in my view, I have a whole HUGE clump of
> 
> \<% if current\_user %\> and then \<% if current\_admin %\> and then \<% if  
> current\_teacher %\> and then all that other stuff in my view.
> 
> I have a ginourmous chunk of if and else statements in my views… is  
> there a way to make it prettier? The if and else’s almost all have the  
> same functions, expect for some minor changes.
> 
> Like this-
> 
> \<% if current\_user %\>  
> \<%= link\_to “Create a new BLAH”, new\_blah\_path %\>
> 
> \<% end %\>
> 
> \<% if current\_admin %\>  
> \<%= link\_to “Create a new ADMINBLAH”, new\_adminblah\_path %\>
> 
> \<% end %\>
> 
> BUT I have a LOT LOTLOT of them… all basically doing the same  
> things, with the path changed.
> 
> Now, in this situation, how would david h hansson do it? 🙂 Would he  
> create a helper of some sort? Because I feel like I’m repeating myself  
> a lot in the code. What happened to keeping it DRY ??? 😛
> 
> So how would i go about doing this?

I think you’ve answered your own question. What exactly do you need  
help with?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 21, 2010, 12:11am UTC](https://www.ruby-forum.com/t/spring-cleaning/187275/3 "2010-04-21T00:11:41Z")

</div>

my question was how exactly to make that helper method

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 21, 2010, 12:14am UTC](https://www.ruby-forum.com/t/spring-cleaning/187275/4 "2010-04-21T00:14:15Z")

</div>

[Please quote when replying. It will make the context clearer.]

David Z. wrote:

> my question was how exactly to make that helper method

Try it. What part don’t you understand?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 21, 2010, 10:15am UTC](https://www.ruby-forum.com/t/spring-cleaning/187275/5 "2010-04-21T10:15:13Z")

</div>

\<% if current\_user %\>  
\<%= link\_to “Create a new BLAH”, new\_blah\_path %\>  
\<% end %\>

To reduce this to one line you can do  
\<%= link\_to(“Create a new BLAH”, new\_blah\_path) If(currrent\_user) %\>

Authorization with something like CanCan or Declarative Authorization  
is still going to require an if-test in the view if you only want to  
display links that the user are actually allowed to access.

---

<div class="post-metadata">

**Author:** ![davidz101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@davidz101](https://www.ruby-forum.com/u/davidz101)\
**Post date:** [April 21, 2010, 10:00am UTC](https://www.ruby-forum.com/t/spring-cleaning/187275/6 "2010-04-21T10:00:34Z")

</div>

David,

I’ve recently been doing a lot of apps that require users being logged  
in and different users having different permissions in the apps. If an  
entire controller requires a user being logged in, set a before  
filter. If your app allows non logged in visitors or you want all of  
your admin stuff mixed in with regular user views (as opposed to admin  
controllers), checkout something like

> **[\#192 Authorization with CanCan - RailsCasts](http://railscasts.com/episodes/192-authorization-with-cancan)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

to control access based on rules or user roles. I used to do a ton or  
if statements for this, but cancan has saved me a ton of time and  
patched a lot of holes I probably left open before. It has nice  
helpers for the view and controller layers of your app, and can  
greatly simplify things if you’re using restful controllers.

I’ve pretty much used every feature Ryan has made available in the  
gem, so let me know if you have any questions.

Happy coding,

Matt
