【问题标题】:2 render templates (from devise) on one page (rails 3)2 个渲染模板(来自设计)在一页上(rails 3)
【发布时间】:2011-07-01 09:35:28
【问题描述】:

我已经为我的 rails 应用程序安装了设计,我可以转到登录页面或注册页面。但我希望它们都出现在欢迎页面上......

所以我制作了一个welcome_page_controller.rb,其功能如下:

    class WelcomePageController < ApplicationController
  def index
    render :template => '/devise/sessions/new'
    render :template => '/devise/registration/new'

  end
end

但是当我进入欢迎页面时,我得到了这个错误:

NameError in Welcome_page#index

Showing /Users/tboeree/Dropbox/rails_projects/rebasev4/app/views/devise/sessions/new.html.erb where line #5 raised:

undefined local variable or method `resource' for #<#<Class:0x104931c>:0x102749c>
Extracted source (around line #5):

2: <% @header_title = "Login" %>
3: 
4: 
5: <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
6:   <p><%= f.label :email %><br />
7:   <%= f.email_field :email %></p>
8: 

有人知道这个问题的解决方案吗?提前致谢!

是否与缺少资源功能有关?在welcome_page 控制器中?它可能在设计控制器中的某个地方...?

问候, 泰斯

【问题讨论】:

    标签: ruby-on-rails-3 controller devise


    【解决方案1】:

    这是我设法做到的。

    我已在home#index 中填写了注册表

    我的文件:

    view/home/index.html.erb

    <%= render :file => 'registrations/new' %>
    

    helper/home_helper.rb

    module HomeHelper
      def resource_name
        :user
      end
    
      def resource
        @resource = session[:subscription] || User.new
      end
    
      def devise_mapping
        @devise_mapping ||= Devise.mappings[:user]
      end
    
      def devise_error_messages!
        return "" if resource.errors.empty?
    
        messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
        sentence = I18n.t("errors.messages.not_saved",
                          :count => resource.errors.count,
                          :resource => resource_name)
    
        html = <<-HTML
    <div id="error_explanation">
    <h2>#{sentence}</h2>
    <ul>#{messages}</ul>
    </div>
    HTML
    
        html.html_safe
      end
    
    end
    

    您需要该部分,因为 Devise 使用名为 resource 的东西,并且应该定义它,以便您可以在任何地方调用您的 registration#new

    这样,你应该可以注册了。但是,我需要在同一页面上显示错误。这是我添加的内容:

    layout/home.html.erb(索引视图使用的布局)

    <% flash.each do |name, msg| %>
    
      # New code (allow for flash elements to be arrays)
      <% if msg.class == Array %>
        <% msg.each do |message| %>
          <%= content_tag :div, message, :id => "flash_#{name}" %>
        <% end %>
      <% else %>
    
        # old code
        <%= content_tag :div, msg, :id => "flash_#{name}" %>
    
      <% end %> #don't forget the extra end
    <% end %>
    

    我找到了这个代码here

    这是我创建的:如果在会话中无效,我会保存我的资源对象,这样用户就不必再次填写每个字段。我想存在更好的解决方案,但它有效,对我来说已经足够了;)

    controller/registration_controller.rb

    def create
        build_resource
    
        if resource.save
          if resource.active_for_authentication?
            # We delete the session created by an incomplete subscription if it exists.
            if !session[:subscription].nil?
              session[:subscription] = nil
            end
    
            set_flash_message :notice, :signed_up if is_navigational_format?
            sign_in(resource_name, resource)
            respond_with resource, :location => redirect_location(resource_name, resource)
          else
            set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
            expire_session_data_after_sign_in!
            respond_with resource, :location => after_inactive_sign_up_path_for(resource)
          end
        else
          clean_up_passwords(resource)
          # Solution for displaying Devise errors on the homepage found on:
          # https://stackoverflow.com/questions/4101641/rails-devise-handling-devise-error-messages
          flash[:notice] = flash[:notice].to_a.concat resource.errors.full_messages
          # We store the invalid object in session so the user hasn't to fill every fields again.
          # The session is deleted if the subscription becomes valid.
          session[:subscription] = resource
          redirect_to root_path #Set the path you want here
        end
      end
    

    我想我没有忘记任何代码。随意使用任何你需要的东西。

    此外,您可以在同一页面中添加您的登录表单(类似这样:)

    <%= form_for("user", :url => user_session_path) do |f| %>  
      <%= f.text_field :email %>      
      <%= f.password_field :password %>
      <%= f.submit 'Sign in' %>
      <%= f.check_box :remember_me %>
      <%= f.label :remember_me %>
      <%= link_to "Forgot your password?", new_password_path('user') %>
    <% end %>
    

    干杯!

    【讨论】:

    • 哇非常感谢!!我会尽快尝试!你能说出你为什么在你的脚本中使用这个代码:html =
    • 其实是Devise Helper自己提供的代码。你可以找到代码here
    • 我已经尝试过了,它可以工作......只是我丢失了我的 css?我使用rails 3.1rc4,如果我将devise.css.scss 复制/粘贴到welcome_page.css.scss 它不起作用?当我输入无效数据时,它将转到 /users/registration... 但也许这与我的路由有关,我认为我必须将主页重定向到欢迎页面?最好的问候,
    • 呃?我发布的代码与 CSS 无关,所以也许检查一下你的基础知识(HTML/CSS)?您必须更改该行 redirect_to root_path #Set the path you want here 以在发生错误时选择重定向路径。
    • 是的,你说得对……我在 application_controller.rb 中有一个布局方向,它指向了错误的布局!我会将重定向更改为链接,然后它应该可以工作!再次感谢!问候,Thijs
    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 2012-10-15
    • 2017-08-06
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多