# Login and logout doesn't work in different actions

**URL:** <https://www.ruby-forum.com/t/login-and-logout-doesnt-work-in-different-actions/150337>\
**Category:** Rails\
**Created:** [October 7, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/login-and-logout-doesnt-work-in-different-actions/150337 "2008-10-07T14:33:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shawn\_Tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eada6e/32.png) [@Shawn\_Tang](https://www.ruby-forum.com/u/Shawn_Tang)\
**Post date:** [October 7, 2008, 2:33pm UTC](https://www.ruby-forum.com/t/login-and-logout-doesnt-work-in-different-actions/150337/1 "2008-10-07T14:33:28Z")

</div>

Hi,I am trying to create a user autentication system. Normally the  
user autentication system works fine but now i want to include the  
login and logout in the header of the application. so that u can login  
and logout in any part of the application

this it the weird part of the problem. let’s say i am in articles/  
search then i login and logout there is no problem. but when i go to  
articles/show i get

* * *

Processing GroceriesController#7 (for 192.168.71.2 at 2008-10-07  
20:22:14) [POST]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlZmYyODM1OGM3OGM5ODA5NThhYWM0MDIwMGM0Y2E3  
ZDg6DHVzZXJfaWRpByIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxh  
c2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==–8a4d25e872b1a1d27e663069f94087a6245f75aa  
Parameters:  
{“authenticity\_token”=\>“2f8d0d7adeea4495c27d1c045a14675dad7294f8”,  
“action”=\>“7”, “controller”=\>“groceries”}

## ActionController::UnknownAction (No action responded to 7):

which does not make sense cause the logout link is linked to  
the :action :destroy

Next i try logging out in search and logging in inside :show and  
logging out inside :show as well and everything works.

* * *

here is the Layout :

\<%= stylesheet\_link\_tag ‘star’ %\>  
\<%= javascript\_include\_tag :defaults, “jquery” %\>

 \<%= render :partial =\> 'login\_panel'%\> 

```
  <%= yield %>

```

Partial view “\_login\_panel.html.erb”:

  \<% if logged\_in? && session[:user\_id]%\>

```
  <%= User.find(session[:user_id]).login %> ,you are logged in! || 

```

 

\<  
%= link\_to\_remote “logout”, :url =\> {:controller =\>  
‘articles’, :action =\> ‘destroy’} %\>

 

```
  <% else %>

```

 

\<% form\_remote\_tag :url =\> {:controller =\> “articles”, :action =\>  
“create”} do %\>  
Login:  
\<%= text\_field\_tag :login, params[:login] %\>

 

Password:  
\<%= password\_field\_tag ‘password’ %\>

 

Remember me for 2 weeks:  
\<%= check\_box\_tag ‘remember\_me’ %\>

 

\<%= submit\_tag ‘Log in’ %\>  
\<% end -%\>

 

```
  <% end -%>

```

 

* * *

Articles controller:

class ArticlesController \< ApplicationController

protect\_from\_forgery :only =\> [:create, :delete, :update]

def create  
password\_authentication(params[:login], params[:password])  
end

def rate  
@articles = Article.find(params[:id])  
@articles.rate(params[:stars], User.find(session[:user\_id]))

# some page update here …

```
      respond_to do |format|

        format.js {
                render :update do |page|
    page.replace_html 'ratingdiv', :partial => 'grocery_with_rating'

                end
          }
        end

```

end

def search  
articles\_per\_page = 12

```
  @articles = Article.search params[:query], {:page =>

```

params[:page], :per\_page =\> articles\_per\_page}

```
  if request.xml_http_request?
        render :partial => "search", :layout => false
  end
  end

```

def show  
@articles = Article.find(params[:id])

```
  respond_to do |format|
    format.html # index.html.erb
    format.xml { render :xml => @articles }
end

```

end

def destroy

```
self.current_user.forget_me if logged_in?
cookies.delete :auth_token
reset_session
render :update do |page|
  page.replace_html "login_panel", :partial => "login_panel"
end

```

end

protected

# Updated 2/20/08

def password\_authentication(login, password)  
user = User.authenticate(login, password)  
if user == nil  
failed\_login(“Your username or password is incorrect.”)  
elsif user.activated\_at.blank?  
failed\_login(“Your account is not active, please check your  
email for the activation code.”)  
elsif user.enabled == false  
failed\_login(“Your account has been disabled.”)  
else  
self.current\_user = user  
successful\_login  
end  
end

private

def failed\_login(message)  
flash.now[:error] = message  
render :action =\> ‘new’  
end

def successful\_login  
if params[:remember\_me] == “1”  
self.current\_user.remember\_me  
cookies[:auth\_token] = { :value =\>  
self.current\_user.remember\_token , :expires =\>  
self.current\_user.remember\_token\_expires\_at }  
else  
self.current\_user.remember\_me2  
cookies[:auth\_token] = { :value =\>  
self.current\_user.remember\_token , :expires =\>  
self.current\_user.remember\_token\_expires\_at }

```
end

  flash[:notice] = "Logged in successfully"
  return_to = session[:return_to]
  if return_to.nil?
        render :update do |page|
  page.replace_html "login_panel", :partial => "login_panel"
end
  else
    redirect_to return_to
  end

```

end  
end

---

<div class="post-metadata">

**Author:** ![Shawn\_Tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eada6e/32.png) [@Shawn\_Tang](https://www.ruby-forum.com/u/Shawn_Tang)\
**Post date:** [October 7, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/login-and-logout-doesnt-work-in-different-actions/150337/2 "2008-10-07T14:38:11Z")

</div>

Sorry the error that i get should be like this:  
Processing ArticlesController#7 (for 192.168.71.2 at 2008-10-07  
20:22:14) [POST]  
Session ID:  
BAh7CDoMY3NyZl9pZCIlZmYyODM1OGM3OGM5ODA5NThhYWM0MDIwMGM0Y2E3  
ZDg6DHVzZXJfaWRpByIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxh  
c2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7AA==–8a4d25e872b1a1d27e663069f94087a6245f75aa  
Parameters:  
{“authenticity\_token”=\>“2f8d0d7adeea4495c27d1c045a14675dad7294f8”,  
“action”=\>“7”, “controller”=\>“articles”}

## ActionController::UnknownAction (No action responded to 7):

---

<div class="post-metadata">

**Author:** ![Shawn\_Tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eada6e/32.png) [@Shawn\_Tang](https://www.ruby-forum.com/u/Shawn_Tang)\
**Post date:** [October 7, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/login-and-logout-doesnt-work-in-different-actions/150337/3 "2008-10-07T18:25:36Z")

</div>

after more inspection i have found that it is the link\_to\_remote that  
has lost its :url. it seems to be picking up the url of the action  
page. Does anyone know why?

---

<div class="post-metadata">

**Author:** ![Shawn\_Tang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eada6e/32.png) [@Shawn\_Tang](https://www.ruby-forum.com/u/Shawn_Tang)\
**Post date:** [October 7, 2008, 7:32pm UTC](https://www.ruby-forum.com/t/login-and-logout-doesnt-work-in-different-actions/150337/4 "2008-10-07T19:32:29Z")

</div>

Never mind i figured it out
