2016-01-22 165 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值穿过帮手。