Thi*_*ijs 1 controller devise ruby-on-rails-3
我为我的rails应用安装了设备,我可以进入登录页面或注册页面.但我希望他们都在欢迎页面上...
所以我用以下函数创建了一个welcome_page_controller.rb:
class WelcomePageController < ApplicationController
def index
render :template => '/devise/sessions/new'
render :template => '/devise/registration/new'
end
end
Run Code Online (Sandbox Code Playgroud)
但是当我进入欢迎页面时,我收到此错误:
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:
Run Code Online (Sandbox Code Playgroud)
有谁知道这个问题的解决方案?提前致谢!
它是否与缺少资源功能有关?在welcome_page控制器?它可能在设计控制器的某个地方......?
此致,Thijs
这就是我设法做到的方式.
我在我的网站上放了一个注册表格 home#index
我的文件:
查看/家/ index.html.erb
<%= render :file => 'registrations/new' %>
Run Code Online (Sandbox Code Playgroud)
助手/ 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
Run Code Online (Sandbox Code Playgroud)
你需要那个部分,因为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 %>
Run Code Online (Sandbox Code Playgroud)
我在这里找到了这段代码
这是我创建的内容:如果会话中的文件无效,我保存了我的资源对象,这样用户就不会再次填充每个字段.我想一个更好的解决方案存在,但它的工作原理对我来说已经足够了;)
控制器/ 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
Run Code Online (Sandbox Code Playgroud)
我想我没有忘记任何代码.随意使用您需要的任何东西.
此外,您可以在同一页面中添加您的登录表单(类似:)
<%= 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 %>
Run Code Online (Sandbox Code Playgroud)
干杯!
| 归档时间: |
|
| 查看次数: |
2797 次 |
| 最近记录: |