Spring Cleaning

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? :slight_smile: 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 ??? :stuck_out_tongue:

So how would i go about doing this?

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? :slight_smile: 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 ??? :stuck_out_tongue:

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
[email protected]

my question was how exactly to make that helper method

[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
[email protected]

<% 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.

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

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