1
votes

I saw a few tutorials about setting up devise for all pages using helper, but I can't achieve the same result, here are my code.

I'm following this tutorial but it's not working: https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

I have a controller for my pages(now I only have the landing page)

pages_controller.rb

    class PagesController < ApplicationController


    def landing
    end
end

and I have a header partial in views/layouts/shareds/_header.html.erb where I am calling the devise sign up/sign in.

<div class="col-md-5">

                <div class="signup-header wow fadeInUp">
                    <h3 class="form-title text-center">GET STARTED</h3>
                    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
                          <%= devise_error_messages! %>

                          <div class="field">
                            <%= f.label :email %><br />
                            <%= f.email_field :email, autofocus: true %>
                          </div>

                          <div class="field">
                            <%= f.label :password %>
                            <% if @minimum_password_length %>
                            <em>(<%= @minimum_password_length %> characters minimum)</em>
                            <% end %><br />
                            <%= f.password_field :password, autocomplete: "off" %>
                          </div>

                          <div class="field">
                            <%= f.label :password_confirmation %><br />
                            <%= f.password_field :password_confirmation, autocomplete: "off" %>
                          </div>

                          <div class="actions">
                            <%= f.submit "Sign up" %>
                          </div>
                        <% end %>
                </div>              

            </div>

following the tutorial I did:

application_helper.rb

 module ApplicationHelper
    helper_method :resource_name, :resource, :devise_mapping

  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  include ApplicationHelper
end

I keep getting this error: undefined method `helper_method' for ApplicationHelper:Module

enter image description here

1
helper_method is available in ApplicationController, not view helpers. Move your methods to ApplicationController and use helper_method there. Then the methods will be available in both your controllers and views. - AOG
thank you, that worked. Didn't knew this about the helper_method. - Matheus Mendes

1 Answers

0
votes

Remove helper_method:

 #app/helpers/application_helper.rb`
 module ApplicationHelper
  def resource_name
    :user
  end

  def resource
    @resource ||= User.new
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end
end

As the docs state...

Declare a controller method as a helper.

It's meant to take controller methods and make them available to your views:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   helper_method :current_user

   private

   def current_user
      ...
   end
end

#app/views/layouts/application.html.erb
<%= link_to "Profile", user_profile_path if current_user %>

To add to the comments, the helper methods, especially for Devise, are there to provide supportive functionality for your views:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>

As can be seen above, the resource & resource_name values are pulled through the helper.