2016-01-22 155 views
1

我看到了一些關於使用助手爲所有頁面設置設計的教程,但我無法達到相同的效果,這裏是我的代碼。設計註冊登陸頁面

我下面這個教程,但它不工作:https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

我有我的網頁(我現在只擁有着陸頁)

pages_controller.rb控制器

class PagesController < ApplicationController 


    def landing 
    end 
end 

和我有一個標頭部分在views/layouts/shareds/_header.html.erb我打電話給設計註冊/登錄。

<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> 

教程我做了以下內容:

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 

我不斷收到此錯誤:未定義的方法`是helper_method 'for ApplicationHelper:模塊

enter image description here

+2

''helper_method'在ApplicationController中可用,而不是查看幫助器。將你的方法移動到ApplicationController並在那裏使用'helper_method'。然後這些方法將在您的控制器和視圖中可用。 – AOG

+0

謝謝你,工作。不知道這個helper_method。 –

回答

0

刪除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 

由於docs狀態...

Declare a controller method as a helper.

這意味着要採取controller方法,讓他們提供給您的觀點:

#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 %> 

要添加到的意見,helper methods,特別是對Devise,在那裏爲你的觀點提供支持功能:

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

正如上面可以看出,resource & resource_name值穿過幫手。