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 ??? :P So how would i go about doing this? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
David Zhu 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 ??? :P > > 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 marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
my question was how exactly to make that helper method On Apr 20, 6:00 pm, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> David Zhu 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 ??? :P > > > 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-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
[Please quote when replying. It will make the context clearer.] David Zhu wrote:> my question was how exactly to make that helper methodTry it. What part don''t you understand? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 http://railscasts.com/episodes/192-authorization-with-cancan 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 On Apr 20, 3:18 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 ??? :P > > So how would i go about doing this? > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
<% 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. On Apr 21, 8:44 am, Matt <orupva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 likehttp://railscasts.com/episodes/192-authorization-with-cancan > 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 > > On Apr 20, 3:18 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 ??? :P > > > So how would i go about doing this? > > > -- > > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.