2011-07-01 6 views
0

レールアプリにdeviseをインストールしました。サインインページまたはサインアップページに行くことができます。2つのページ(レール3)に2つのレンダリングテンプレート(レール3)

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

    end 
end 

しかし、私は、ウェルカムページに行くとき、私はこのエラーを取得:しかし、私は

は、だから私は、次の機能を備えたwelcome_page_controller.rbを作った...ウェルカムページにそれらの両方をしたいです:

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コントローラで?それはおそらくdevisコントローラのどこかにあるでしょう...?ここで

よろしく、 タイス

答えて

4

は、私はそれをしなかったために管理方法を説明します。

ビュー/ホーム/ index.html.erb

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

ヘルパー/ home_helper.rb:

私は私のhome#index

マイファイルにサインアップフォームを入れています

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 

あなたはresourceと呼ばれるものでDeviseが動作し、どこにでも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を失った?私はレール3.1rc4で動作し、もし私がコピーする/ devise.css.scssをwelcome_page.css.scssに貼り付けると動作しませんか?そして、私は有効なデータを入力しないと、/ users/registrationに行きます。しかし、それは私のルーティングと関係があり、私はウェルカムページに家にリダイレクトする必要がありますか?よろしくお願いします。 – Thijs