I’m currently working on a hobby project in Rails, and stumbled upon a what I will think is a common problem in Rails. I’ve been through a couple of chapters plus and a Lynda tutorial, but I haven’t found any best practices for this problem. Maybe the online community can help me.
In my application.html.erb I have added the following code:
<!DOCTYPE html>
<html>
<head>
<title>SioMeny</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="wrapper">
<header>
<h1>SiO Menysystem</h1>
</header>
<div id="site_menu">
<%= render :partial => "shared/admin_menu" if admin_logged_in? %>
<%= render :partial => "shared/user_menu" if logged_in? %>
<%= render :partial => "shared/site_menu" %>
</div><!-- div#site_menu -->
<div id="main">
<%= render :partial => "shared/flash", :locals => {:flash => flash} unless flash.nil? %>
<%= yield %>
</div>
</div><!-- div#wrapper -->
</body>
</html>
My problem starts in the #site_menu and in the loading of the different partials. What I try to achieve here is a menu that is expanding according to the privileges a user may have. The logged_in? and the admin_logged_in? Is located in the application_controller.
class ApplicationController < ActionController::Base
protect_from_forgery
def login_required
unless logged_in?
flash[:notice] = "Du må være logget inn for å se denne siden"
redirect_to :controller => "user", :action => "login"
end
end
def admin_required
unless admin_logged_in?
flash[:notice] = "Du må være logget inn som administrator for å se denne siden"
redirect_to :controller => "user", :action => "login"
end
end
def logged_in?
if session[:user_id]
return true
end
return false
end
def admin_logged_in?
if logged_in?
user = User.find_by_id[:user_id]
if user.is_admin?
return true
end
end
return false
end
end
At this point I get an error:
ArgumentError in Site#index
Showing /Users/ola/Documents/Coding/SioMeny/app/views/layouts/application.html.erb where line #15 raised:
wrong number of arguments (0 for 1)
Extracted source (around line #15):
12: <h1>SiO Menysystem</h1>
13: </header>
14: <div id="site_menu">
15: <%= render :partial => "shared/admin_menu" if admin_logged_in? %>
16: <%= render :partial => "shared/user_menu" if logged_in? %>
17: <%= render :partial => "shared/site_menu" %>
18: </div><!-- div#site_menu -->
Rails.root: /Users/ola/Documents/Coding/SioMeny
Any help is very welcome! I have also tried to get both the logged_in functions to set an instance variable which of type Boolean which is checked in the view, this worked, but not all the time since they were depending on when they were set. I also tried to make similar methods in a helper and call them from the view, but that didn’t work either.
I think your problem is this line in admin_logged_in?
using the brackets at the end of find_by_id, doesn’t pass the symbol in as a parameter, it just directly calls find_by_id with no parameters. And find_by_id expects a parameter. Assuming you had a :user_id available in your params hash, you could do something like this instead: