Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Devise Helper methods inside cell controllers and views #23

Closed
bhavinkamani opened this issue Nov 19, 2010 · 9 comments
Closed

Comments

@bhavinkamani
Copy link

Hi,

I am using Devise as authentication plugin. Since login box is common
across all my pages, I have extracted that out as a cell. However, I
am getting errors while accessing devise helper methods such as
user_signed_in? etc. It is giving me undefined method error. Based on
the last post, I tried inserting "include
Devise::Controllers::Helpers". It still is giving me errors.
Here's the cell controller and the view code

app/cells/authenticate_cell.rb

class AuthenticateCell < Cell::Rails 
    include Devise::Controllers::Helpers 
    def login 
      @user = User.new 
      render 
    end 
end 

app/cells/authenticate/login.html.rb

<div class="container login_info"> 
  <% if user_signed_in? %> 
    Signed in as <%= current_user.email %>. Not you? 
    <%= link_to "Sign out", destroy_user_session_path %> 
  <% else %> 
  <%= form_for(resource, :as => resource_name, :url => 
session_path(resource_name)) do |f| %> 
    <p><%= f.label :email %><br /> 
    <%= f.email_field :email %></p> 
    <p><%= f.label :password %><br /> 
    <%= f.password_field :password %></p> 
    <% if devise_mapping.rememberable? -%> 
      <p><%= f.check_box :remember_me %> <%= f.label :remember_me %></ 
p> 
    <% end -%> 
    <p><%= f.submit "Sign in" %></p> 
    <% end %> 
  <% end %> 
</div> 

Thanks in advance.

@mixr
Copy link

mixr commented Nov 20, 2010

i have the same problems in a rails 3 app and just use signed_in?(:user) for the moment.

when i tried to reproduce the problem in a blank rails 3 app, everything worked fine just by including "include Devise::Controllers::Helpers" in the Cell.

i have no idea right now which part of my app-setup is responsible for this.

@bhavinkamani
Copy link
Author

Thanks for your inputs. While signed_in?(:user) seems to be working fine. I am getting stuck with resource_name while inserting login form inside cell view. I have included Devise::Controllers::Helpers in the cell. I am trying to keep the view code as close to devise generated view code as possible. Hence I am not directly using user model.

Did you happen to face that problem?

@bhavinkamani
Copy link
Author

Apart from "include Devise::Controllers::Helpers", inserting Devise::Controllers::Helpers.define_helpers(User) helps me access rest of the devise helper methods. Not sure if this is the best way or the cleaner way to access devise helper methods.

@apotonick
Copy link
Member

I got it working by calling

helper_method :current_user

and that's what José recommended, too!

@bhavinkamani
Copy link
Author

Thanks it works great... looks much cleaner too..

@mixr
Copy link

mixr commented Nov 23, 2010

hey,

did you try with a fresh rails 3 app? it's working without any extras, just include
include Devise::Controllers::Helpers
you can try the example app here: https://github.com/mixr/cells_devise

@bhavinkamani
Copy link
Author

Hi,

Yep I tried and yes it indeed works without any additional line... Its kind of strange because now when I run my app after removing "helper_method :current_user" its recognizing user_signed_in? method. I have still not figured out why it was giving me error earlier. But good news is that it now works with one line code and thats 50% improvement over 2 lines :-) Thanks a ton.

@fsmanuel
Copy link

thax!
class TransactionWidget < Apotomo::Widget
include Devise::Controllers::Helpers

helper_method :current_user

did the trick!

@johanbaaij
Copy link

Hi, i'm trying the following:

app/cells/main_menu_cell.rb

class MainMenuCell < Cell::Rails
    include Devise::Controllers::Helpers
    helper_method :current_user

    def display
        render
    end
end

display.html.haml

- if user_signed_in?
    %li
        = link_to 'Logout', destroy_user_session_path, :method=>'delete'
- else
    %li
        = link_to 'Login', new_user_session_path

But i'm getting a undefined method `user_signed_in?'

What could I be doing wrong?
Cheers!

Edit:

duh. I get it :). Had to use
helper_method :user_signed_in?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants