2011-07-01 17 views
0

我已經爲我的rails應用程序安裝了設計,我可以進入登錄頁面或註冊頁面。但我想他們兩個一個歡迎頁面上...2在一個頁面上呈現模板(來自devise)(導軌3)

所以我做了具有以下功能的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控制器中?它可能在設計控制器的某個地方......?

問候, 泰斯

回答

4

下面是如何成功地做到了。

我已經把註冊表格在我home#index

我的文件:

視圖/家庭/ index.html.erb

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

幫手/ 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任何地方。

像這樣,你應該可以註冊。但是,我需要在同一頁面上顯示錯誤。下面是我添加了什麼:

佈局/ 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

而且這裏的東西我創建:我救了我的資源對象如果在會話中無效,以便用戶不必再填充每個字段。我想一個更好的解決方案存在,但它的工作原理,它足以讓我)

控制器/ 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 %> 

乾杯!

+0

非常感謝你!我會盡快嘗試!你能告訴你爲什麼使用這段代碼:html = << - 腳本中的HTML?再次感謝,最誠摯的問候,Thijs – Thijs

+0

這實際上是由Devise Helper本身提供的代碼。你可以找到代碼[這裏](https://github.com/plataformatec/devise/blob/30f9da9d71640025f78f46f359e371fcd02add66/app/helpers/devise_helper.rb) – Lucas

+0

我試過了,它的工作原理...只有我失去了我的CSS ?我使用rails 3.1rc4,如果我將devise.css.scss複製/粘貼到welcome_page.css.scss,它不起作用?而當我輸入無效的數據時,它會去/用戶/註冊...但也許這與我的路由,我必須重定向到歡迎頁面,我想?最好的問候, – Thijs