I want to protect a specific page in a controller from access only if the user is logged in it is the add.html page for adding questions, the index.html page is used to show questions that are asked but I want to to remain public. How do I make it that when someone clicks on ask a question from index that it checks for login and if not logged in has flash notice saying you must login and redirect at same time to login page.
New error on index page:
NoMethodError in Car#index
Showing app/views/car/index.html.erb where line #3 raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #3):
1: <h2>Current List of Open Questions</h2>
2: <dl>
3: <% @car.each do |car| %>
4: <dd>
5: <%= car.name %><br />
6: <%= car.description %><br />
You want to add a before_filter to ensure the user is logged in before running the protected actions. Here’s a simple example – naturally your implementation details depend on how you rolled your own auth.
In your application controller (assuming you’re storing the id of the current logged in user in session[:user_id]):
and then in the controller where you are limiting access: